From 3b0ab8d042216f57685bcd06ad35e7e01d3742d2 Mon Sep 17 00:00:00 2001 From: Julian Smith Date: Tue, 27 Feb 2024 23:14:38 +0000 Subject: [PATCH] setup.py: Only overwrite MuPDF's include/mupdf/fitz/config.h on Windows. This avoids modifying a local MuPDF tree when developing. There is no need on non-Windows because we already predefine TOFU_CJK_EXT in XCFLAGS. On Windows there is no equivalent to XCFLAGS so for simplicity we continue to overwrite MuPDF's config.h. For completeness we now also predefine TOFU_CJK_EXT in XCXXFLAGS for the C++ bindings compiles, although in practise this will not make any difference. Also modify env_add() to default to adding to os.environ[name] if name is not in env, so we always append to existing environment variable if available. --- setup.py | 43 +++++++++++++++++++++++++++++++------------ 1 file changed, 31 insertions(+), 12 deletions(-) diff --git a/setup.py b/setup.py index 9048a2ce..cfb1d9b9 100755 --- a/setup.py +++ b/setup.py @@ -522,20 +522,35 @@ def build(): # env_extra = dict() if mupdf_local: - from_ = f'{g_root}/src_classic/_config.h' - to_ = f'{mupdf_local}/include/mupdf/fitz/config.h' + if windows: + from_ = f'{g_root}/src_classic/_config.h' + to_ = f'{mupdf_local}/include/mupdf/fitz/config.h' if os.environ.get('PYMUPDF_SETUP_MUPDF_OVERWRITE_CONFIG') == '0': # Use MuPDF default config. - log( f'Not copying {from_} to {to_}.') + if windows: + log( f'Not copying {from_} to {to_}.') + else: + log( f'Not predefining TOFU_CJK_EXT.') else: - # Use our special config in MuPDF. - log( f'Copying {from_} to {to_}.') - shutil.copy2( from_, to_) - # Tell the MuPDF shared-library build to exclude large unused font + # Our config differs from MuPDF's default in that it defines TOFU_CJK_EXT + # This tells the MuPDF shared-library build to exclude large unused font # files such as resources/fonts/han/SourceHanSerif-Regular.ttc. - env_extra[ 'XCFLAGS'] = '-DTOFU_CJK_EXT' - s = os.stat( f'{to_}') - log( f'{to_}: {s} mtime={time.strftime("%F-%T", time.gmtime(s.st_mtime))}') + # + if windows: + # No equivalent to XCFLAGS. We could create an override.props + # file and run devenv.com with + # `-p:ForceImportBeforeCppTargets=$(SolutionDir)override.props`, + # but for now we just overwrite MuPDF's config.h. + log( f'Copying {from_} to {to_}.') + shutil.copy2( from_, to_) + s = os.stat( f'{to_}') + log( f'{to_}: {s} mtime={time.strftime("%F-%T", time.gmtime(s.st_mtime))}') + else: + # By predefining TOFU_CJK_EXT here, we don't need to modify + # MuPDF's include/mupdf/fitz/config.h. + log( f'Setting XCFLAGS and XCXXFLAGS to predefine TOFU_CJK_EXT.') + env_add(env_extra, 'XCFLAGS', '-DTOFU_CJK_EXT') + env_add(env_extra, 'XCXXFLAGS', '-DTOFU_CJK_EXT') # Build MuPDF shared libraries. # @@ -644,17 +659,21 @@ def add( ret, from_, to_): return ret -def env_add(env, name, value, sep=' ', prefix=False, verbose=False): +def env_add(env, name, value, sep=' ', prepend=False, verbose=False): ''' Appends/prepends `` to `env[name]`. + + If `name` is not in `env`, we use os.environ[nane] if it exists. ''' v = env.get(name) if verbose: log(f'Initally: {name}={v!r}') + if v is None: + v = os.environ.get(name) if v is None: env[ name] = value else: - if prefix: + if prepend: env[ name] = f'{value}{sep}{v}' else: env[ name] = f'{v}{sep}{value}'