|
| 1 | +#! /usr/bin/env python |
| 2 | +# |
| 3 | +# Copyright (C) 2012-2025 Ben Kurtovic <[email protected]> |
| 4 | +# |
| 5 | +# Permission is hereby granted, free of charge, to any person obtaining a copy |
| 6 | +# of this software and associated documentation files (the "Software"), to deal |
| 7 | +# in the Software without restriction, including without limitation the rights |
| 8 | +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 9 | +# copies of the Software, and to permit persons to whom the Software is |
| 10 | +# furnished to do so, subject to the following conditions: |
| 11 | +# |
| 12 | +# The above copyright notice and this permission notice shall be included in |
| 13 | +# all copies or substantial portions of the Software. |
| 14 | +# |
| 15 | +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 16 | +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 | +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 18 | +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 19 | +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 20 | +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 21 | +# SOFTWARE. |
| 22 | + |
| 23 | +from __future__ import annotations |
| 24 | + |
| 25 | +import glob |
| 26 | +import os |
| 27 | +import sys |
| 28 | +from enum import Enum |
| 29 | + |
| 30 | +from setuptools import Extension, setup |
| 31 | +from setuptools.command.build_ext import build_ext |
| 32 | + |
| 33 | + |
| 34 | +class UseExtension(Enum): |
| 35 | + REQUIRED = 1 |
| 36 | + OPTIONAL = 2 |
| 37 | + IGNORED = 3 |
| 38 | + |
| 39 | + |
| 40 | +if "WITH_EXTENSION" in os.environ: |
| 41 | + if "WITHOUT_EXTENSION" in os.environ: |
| 42 | + raise RuntimeError("Cannot set both $WITH_EXTENSION and $WITHOUT_EXTENSION") |
| 43 | + value = os.environ["WITH_EXTENSION"].lower() |
| 44 | + if value in ("1", "true", "yes", "required"): |
| 45 | + use_extension = UseExtension.REQUIRED |
| 46 | + elif value == "optional": |
| 47 | + use_extension = UseExtension.OPTIONAL |
| 48 | + elif value in ("0", "false", "no"): |
| 49 | + use_extension = UseExtension.IGNORED |
| 50 | + else: |
| 51 | + raise RuntimeError( |
| 52 | + f"Unknown value for $WITH_EXTENSION; should be '1', '0', or 'optional', " |
| 53 | + f"but found {value!r}" |
| 54 | + ) |
| 55 | +elif "WITHOUT_EXTENSION" in os.environ: |
| 56 | + value = os.environ["WITHOUT_EXTENSION"].lower() |
| 57 | + if value in ("1", "true", "yes"): |
| 58 | + use_extension = UseExtension.IGNORED |
| 59 | + elif value in ("0", "false", "no"): |
| 60 | + use_extension = UseExtension.REQUIRED |
| 61 | + else: |
| 62 | + raise RuntimeError( |
| 63 | + f"Unknown value for $WITHOUT_EXTENSION; should be '1', or '0', " |
| 64 | + f"but found {value!r}" |
| 65 | + ) |
| 66 | +else: |
| 67 | + use_extension = UseExtension.REQUIRED |
| 68 | + |
| 69 | +if use_extension == UseExtension.IGNORED: |
| 70 | + ext_modules = [] |
| 71 | +else: |
| 72 | + tokenizer = Extension( |
| 73 | + "mwparserfromhell.parser._tokenizer", |
| 74 | + sources=sorted(glob.glob("src/mwparserfromhell/parser/ctokenizer/*.c")), |
| 75 | + depends=sorted(glob.glob("src/mwparserfromhell/parser/ctokenizer/*.h")), |
| 76 | + optional=use_extension == UseExtension.OPTIONAL, |
| 77 | + ) |
| 78 | + ext_modules = [tokenizer] |
| 79 | + |
| 80 | + |
| 81 | +def build_ext_patched(self): |
| 82 | + try: |
| 83 | + build_ext_original(self) |
| 84 | + except Exception: |
| 85 | + print( |
| 86 | + """ |
| 87 | +********** |
| 88 | +Note: To avoid building the C tokenizer extension, set the environment variable \ |
| 89 | +`WITH_EXTENSION=0`. |
| 90 | +This will fall back to a pure-Python tokenizer. |
| 91 | +********** |
| 92 | +""", |
| 93 | + file=sys.stderr, |
| 94 | + ) |
| 95 | + raise |
| 96 | + |
| 97 | + |
| 98 | +build_ext.run, build_ext_original = build_ext_patched, build_ext.run |
| 99 | + |
| 100 | +setup(ext_modules=ext_modules) |
0 commit comments