Skip to content

Commit

Permalink
Move cmake/configure/make code out of shared.py and into the scripts …
Browse files Browse the repository at this point in the history
…themselves. NFC. (emscripten-core#13772)
  • Loading branch information
sbc100 authored Mar 25, 2021
1 parent ac1e6a0 commit 52ed04f
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 76 deletions.
37 changes: 36 additions & 1 deletion emcmake.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@

import sys
from tools import building
from tools import shared
from tools import config
from tools import utils
from subprocess import CalledProcessError


Expand All @@ -22,8 +25,40 @@ def run():
''', file=sys.stderr)
return 1

args = sys.argv[1:]
env = building.get_building_env()

def has_substr(args, substr):
return any(substr in s for s in args)

# Append the Emscripten toolchain file if the user didn't specify one.
if not has_substr(args, '-DCMAKE_TOOLCHAIN_FILE'):
args.append('-DCMAKE_TOOLCHAIN_FILE=' + utils.path_from_root('cmake', 'Modules', 'Platform', 'Emscripten.cmake'))
node_js = config.NODE_JS

if not has_substr(args, '-DCMAKE_CROSSCOMPILING_EMULATOR'):
node_js = config.NODE_JS[0].replace('"', '\"')
args.append('-DCMAKE_CROSSCOMPILING_EMULATOR="%s"' % node_js)

# On Windows specify MinGW Makefiles or ninja if we have them and no other
# toolchain was specified, to keep CMake from pulling in a native Visual
# Studio, or Unix Makefiles.
if utils.WINDOWS and '-G' not in args:
if utils.which('mingw32-make'):
args += ['-G', 'MinGW Makefiles']
elif utils.which('ninja'):
args += ['-G', 'Ninja']

# CMake has a requirement that it wants sh.exe off PATH if MinGW Makefiles
# is being used. This happens quite often, so do this automatically on
# behalf of the user. See
# http://www.cmake.org/Wiki/CMake_MinGW_Compiler_Issues
if utils.WINDOWS and 'MinGW Makefiles' in args:
env = building.remove_sh_exe_from_path(env)

print('configure: ' + shared.shlex_join(args), file=sys.stderr)
try:
building.configure(sys.argv[1:])
shared.check_call(args, env=env)
return 0
except CalledProcessError as e:
return e.returncode
Expand Down
13 changes: 11 additions & 2 deletions emconfigure.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import sys
from tools import building
from tools import shared
from subprocess import CalledProcessError


Expand All @@ -35,12 +36,20 @@ def run():
(but you can run any command instead of configure)''', file=sys.stderr)
return 1

if 'cmake' in sys.argv[1]:
args = sys.argv[1:]

if 'cmake' in args:
print('error: use `emcmake` rather then `emconfigure` for cmake projects', file=sys.stderr)
return 1

env = building.get_building_env()
# When we configure via a ./configure script, don't do config-time
# compilation with emcc, but instead do builds natively with Clang. This
# is a heuristic emulation that may or may not work.
env['EMMAKEN_JUST_CONFIGURE'] = '1'
print('configure: ' + shared.shlex_join(args), file=sys.stderr)
try:
building.configure(sys.argv[1:])
shared.check_call(args, env=env)
return 0
except CalledProcessError as e:
return e.returncode
Expand Down
24 changes: 21 additions & 3 deletions emmake.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@

import sys
from tools import building
from tools import shared
from tools import utils
from subprocess import CalledProcessError


Expand All @@ -40,13 +42,29 @@ def run():
(but you can run any command instead of make)''', file=sys.stderr)
return 1

args = sys.argv[1:]
env = building.get_building_env()

# On Windows prefer building with mingw32-make instead of make, if it exists.
if utils.WINDOWS:
if args[0] == 'make':
mingw32_make = building.which('mingw32-make')
if mingw32_make:
args[0] = mingw32_make

if 'mingw32-make' in args[0]:
env = building.remove_sh_exe_from_path(env)

# On Windows, run the execution through shell to get PATH expansion and
# executable extension lookup, e.g. 'sdl2-config' will match with
# 'sdl2-config.bat' in PATH.
print('make: ' + ' '.join(args), file=sys.stderr)
try:
building.make(sys.argv[1:])
shared.check_call(args, shell=utils.WINDOWS, env=env)
return 0
except CalledProcessError as e:
return e.returncode

return 0


if __name__ == '__main__':
sys.exit(run())
3 changes: 2 additions & 1 deletion tests/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -816,7 +816,8 @@ def get_library(self, name, generated_libs, configure=['sh', './configure'],

print(f'<building and saving {cache_name} into cache>', file=sys.stderr)
if configure is not None:
configure += configure_args
# Avoid += so we don't mutate the default arg
configure = configure + configure_args

return build_library(name, build_dir, output_dir, generated_libs, configure,
make, make_args, self.library_cache,
Expand Down
70 changes: 1 addition & 69 deletions tools/building.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
from .shared import TEMP_DIR
from .shared import CANONICAL_TEMP_DIR, LLVM_DWARFDUMP, demangle_c_symbol_name
from .shared import get_emscripten_temp_dir, exe_suffix, is_c_symbol
from .utils import which, WINDOWS
from .utils import WINDOWS

logger = logging.getLogger('building')

Expand Down Expand Up @@ -272,74 +272,6 @@ def remove_sh_exe_from_path(env):
return env


def handle_cmake_toolchain(args, env):
def has_substr(args, substr):
return any(substr in s for s in args)

# Append the Emscripten toolchain file if the user didn't specify one.
if not has_substr(args, '-DCMAKE_TOOLCHAIN_FILE'):
args.append('-DCMAKE_TOOLCHAIN_FILE=' + path_from_root('cmake', 'Modules', 'Platform', 'Emscripten.cmake'))
node_js = config.NODE_JS

if not has_substr(args, '-DCMAKE_CROSSCOMPILING_EMULATOR'):
node_js = config.NODE_JS[0].replace('"', '\"')
args.append('-DCMAKE_CROSSCOMPILING_EMULATOR="%s"' % node_js)

# On Windows specify MinGW Makefiles or ninja if we have them and no other
# toolchain was specified, to keep CMake from pulling in a native Visual
# Studio, or Unix Makefiles.
if WINDOWS and '-G' not in args:
if which('mingw32-make'):
args += ['-G', 'MinGW Makefiles']
elif which('ninja'):
args += ['-G', 'Ninja']

# CMake has a requirement that it wants sh.exe off PATH if MinGW Makefiles
# is being used. This happens quite often, so do this automatically on
# behalf of the user. See
# http://www.cmake.org/Wiki/CMake_MinGW_Compiler_Issues
if WINDOWS and 'MinGW Makefiles' in args:
env = remove_sh_exe_from_path(env)

return (args, env)


def configure(args):
env = get_building_env()
if 'cmake' in args[0]:
# Note: EMMAKEN_JUST_CONFIGURE shall not be enabled when configuring with
# CMake. This is because CMake does expect to be able to do
# config-time builds with emcc.
args, env = handle_cmake_toolchain(args, env)
else:
# When we configure via a ./configure script, don't do config-time
# compilation with emcc, but instead do builds natively with Clang. This
# is a heuristic emulation that may or may not work.
env['EMMAKEN_JUST_CONFIGURE'] = '1'
print('configure: ' + shared.shlex_join(args), file=sys.stderr)
check_call(args, env=env)


def make(args):
env = get_building_env()

# On Windows prefer building with mingw32-make instead of make, if it exists.
if WINDOWS:
if args[0] == 'make':
mingw32_make = which('mingw32-make')
if mingw32_make:
args[0] = mingw32_make

if 'mingw32-make' in args[0]:
env = remove_sh_exe_from_path(env)

# On Windows, run the execution through shell to get PATH expansion and
# executable extension lookup, e.g. 'sdl2-config' will match with
# 'sdl2-config.bat' in PATH.
print('make: ' + ' '.join(args), file=sys.stderr)
check_call(args, shell=WINDOWS, env=env)


def make_paths_absolute(f):
if f.startswith('-'): # skip flags
return f
Expand Down

0 comments on commit 52ed04f

Please sign in to comment.