66import subprocess
77import os , sys
88import platform
9+ from typing import Optional
910
1011TOP_DIR = os .path .abspath (os .path .dirname (__file__ ))
1112BUILD_DIR = os .path .join (TOP_DIR , "build" )
1213
1314# Get number of CPU cores
1415CPUS = os .cpu_count () or 1
1516
16- def execute (cmd : str ):
17+ def execute (cmd : str , cwd : Optional [ str ] = None ):
1718 popen = subprocess .Popen (cmd , stdout = subprocess .PIPE , stderr = subprocess .STDOUT ,
18- shell = True , text = True )
19+ shell = True , text = True , cwd = cwd )
1920 for stdout_line in iter (popen .stdout .readline , "" ):
2021 sys .stdout .write (stdout_line )
2122 sys .stdout .flush ()
@@ -27,33 +28,31 @@ def execute(cmd: str):
2728
2829def ensure_spidermonkey ():
2930 # Check if SpiderMonkey libs already exist
30- spidermonkey_lib_exist = os .path .exists ("./ _spidermonkey_install/lib" )
31+ spidermonkey_lib_exist = os .path .exists (os . path . join ( TOP_DIR , " _spidermonkey_install/lib" ) )
3132 if spidermonkey_lib_exist :
3233 return
3334
3435 # Build SpiderMonkey
35- execute ("bash ./setup.sh" )
36+ execute ("bash ./setup.sh" , cwd = TOP_DIR )
3637
3738def run_cmake_build ():
3839 os .makedirs (BUILD_DIR , exist_ok = True ) # mkdir -p
39- os .chdir (BUILD_DIR )
4040 if platform .system () == "Windows" :
41- execute ("cmake .. -T ClangCL" ) # use Clang/LLVM toolset for Visual Studio
41+ execute ("cmake .. -T ClangCL" , cwd = BUILD_DIR ) # use Clang/LLVM toolset for Visual Studio
4242 else :
43- execute ("cmake .." )
44- execute (f"cmake --build . -j{ CPUS } --config Release" )
45- os .chdir (TOP_DIR )
43+ execute ("cmake .." , cwd = BUILD_DIR )
44+ execute (f"cmake --build . -j{ CPUS } --config Release" , cwd = BUILD_DIR )
4645
4746def copy_artifacts ():
4847 if platform .system () == "Windows" :
49- execute ("cp ./build/src/*/pythonmonkey.pyd ./python/pythonmonkey/" ) # Release or Debug build
50- execute ("cp ./_spidermonkey_install/lib/mozjs-*.dll ./python/pythonmonkey/" )
48+ execute ("cp ./build/src/*/pythonmonkey.pyd ./python/pythonmonkey/" , cwd = TOP_DIR ) # Release or Debug build
49+ execute ("cp ./_spidermonkey_install/lib/mozjs-*.dll ./python/pythonmonkey/" , cwd = TOP_DIR )
5150 else :
52- execute ("cp ./build/src/pythonmonkey.so ./python/pythonmonkey/" )
53- execute ("cp ./_spidermonkey_install/lib/libmozjs* ./python/pythonmonkey/" )
51+ execute ("cp ./build/src/pythonmonkey.so ./python/pythonmonkey/" , cwd = TOP_DIR )
52+ execute ("cp ./_spidermonkey_install/lib/libmozjs* ./python/pythonmonkey/" , cwd = TOP_DIR )
5453
5554def build ():
56- execute ("git submodule update --init --recursive" )
55+ execute ("git submodule update --init --recursive" , cwd = TOP_DIR )
5756 ensure_spidermonkey ()
5857 run_cmake_build ()
5958 copy_artifacts ()
0 commit comments