@@ -25,88 +25,20 @@ def run(self):
2525 except OSError :
2626 raise RuntimeError ("CMake must be installed" )
2727
28+ if platform .system () not in ("Windows" , "Linux" , "Darwin" ):
29+ raise RuntimeError (f"Unsupported os: { platform .system ()} " )
30+
2831 for ext in self .extensions :
2932 if isinstance (ext , NLOptBuildExtension ):
3033 self .build_extension (ext )
3134
32- @property
33- def nlopt_dir (self ):
34- return Path (__file__ ).parent / "tern" / "nlopt"
35-
3635 @property
3736 def config (self ):
3837 return "Debug" if self .debug else "Release"
3938
40- def cmake_args (self , ext_dir : str ):
41- prefix = "NLOPT_BUILD"
42-
43- args = [
44- f"-DCMAKE_LIBRARY_OUTPUT_DIRECTORY={ ext_dir } " ,
45- f"-DPYTHON_EXECUTABLE={ sys .executable } " ,
46- # don't build for matlab and others
47- "-DNLOPT_GUILE=OFF" ,
48- "-DNLOPT_MATLAB=OFF" ,
49- "-DNLOPT_OCTAVE=OFF" ]
50-
51- if platform .system () == "Windows" :
52- args += [
53- "-LAH" ,
54- f'-DCMAKE_PREFIX_PATH="{ prefix } "' ,
55- f'-DCMAKE_INSTALL_PREFIX="{ prefix } "'
56- ]
57- elif platform .system () in ("Linux" , "Darwin" ):
58- args += [
59- f"-DCMAKE_PREFIX_PATH={ prefix } " ,
60- f"-DCMAKE_INSTALL_PREFIX={ prefix } " ,
61- "-DCMAKE_INSTALL_LIBDIR=lib" ,
62- ]
63- else :
64- raise RuntimeError (f"Unsupported os: { platform .system ()} " )
65-
66- return args
67-
6839 def build_extension (self , ext : Extension ):
6940 # - make sure path ends with delimiter
7041 # - required for auto-detection of auxiliary "native" libs
71- ext_dir = Path (self .get_ext_fullpath (ext .name )).parent .absolute ().as_posix ()
72- if not ext_dir .endswith (os .path .sep ):
73- ext_dir += os .path .sep
74-
75- if platform .system () == "Windows" :
76- self ._build_windows (ext )
77- return
78-
79- exit (1 )
80-
81- cmake_args = [
82- "-DCMAKE_LIBRARY_OUTPUT_DIRECTORY=" + ext_dir ,
83- "-DPYTHON_EXECUTABLE=" + sys .executable ,
84- ]
85-
86- cfg = "Debug" if self .debug else "Release"
87- build_args = ["--config" , cfg ]
88-
89- if platform .system () == "Windows" :
90- cmake_args += [f"-DCMAKE_LIBRARY_OUTPUT_DIRECTORY_{ cfg .upper ()} ={ ext_dir } " ]
91- if sys .maxsize > 2 ** 32 :
92- cmake_args += ["-A" , "x64" ]
93- build_args += (["--" , "/m" ])
94- else :
95- build_args += ["--" , "-j2" ]
96-
97- env = os .environ .copy ()
98- env ["CXXFLAGS" ] = f'{ env .get ("CXXFLAGS" , "" )} -DVERSION_INFO="{ self .distribution .get_version ()} "'
99-
100- build_temp = Path (self .build_temp )
101- build_temp .mkdir (parents = True , exist_ok = True )
102-
103- check_call (["cmake" , ext .sourcedir , * cmake_args ], cwd = self .build_temp , env = env )
104- check_call (["cmake" , "--build" , "." , * build_args ], cwd = self .build_temp )
105-
106- nlopt_py = next (Path (self .build_temp ).rglob ("nlopt.py" ))
107- nlopt_py .rename (Path (ext_dir ) / "__init__.py" )
108-
109- def _build_windows (self , ext : NLOptBuildExtension ):
11042 ext_dir = Path (self .get_ext_fullpath (ext .name )).parent .absolute ()
11143 _ed = ext_dir .as_posix ()
11244 if not _ed .endswith (os .path .sep ):
@@ -115,18 +47,22 @@ def _build_windows(self, ext: NLOptBuildExtension):
11547 build_dir = create_directory (Path (self .build_temp ))
11648
11749 # package builds in 2 steps, first to compile the nlopt package and second to build the DLL
50+ cmd = [
51+ "cmake" ,
52+ "-LAH" ,
53+ f"-DCMAKE_LIBRARY_OUTPUT_DIRECTORY={ _ed } " ,
54+ f"-DPYTHON_EXECUTABLE={ sys .executable } " ,
55+ "-DNLOPT_GUILE=OFF" ,
56+ "-DNLOPT_MATLAB=OFF" ,
57+ "-DNLOPT_OCTAVE=OFF" ,
58+ ext .source_dir .as_posix ()
59+ ]
60+
61+ if platform .system () == "Windows" :
62+ cmd .insert (2 , f"-DCMAKE_LIBRARY_OUTPUT_DIRECTORY_{ self .config .upper ()} ={ _ed } " )
63+
11864 execute_command (
119- cmd = [
120- "cmake" ,
121- "-LAH" ,
122- f"-DCMAKE_LIBRARY_OUTPUT_DIRECTORY={ _ed } " ,
123- f"-DPYTHON_EXECUTABLE={ sys .executable } " ,
124- f"-DCMAKE_LIBRARY_OUTPUT_DIRECTORY_{ self .config .upper ()} ={ _ed } " ,
125- "-DNLOPT_GUILE=OFF" ,
126- "-DNLOPT_MATLAB=OFF" ,
127- "-DNLOPT_OCTAVE=OFF" ,
128- ext .source_dir .as_posix ()
129- ],
65+ cmd = cmd ,
13066 cwd = build_dir ,
13167 env = {
13268 ** os .environ .copy (),
@@ -141,7 +77,7 @@ def _build_windows(self, ext: NLOptBuildExtension):
14177 '--config' ,
14278 self .config ,
14379 "--" ,
144- "-m"
80+ "-m" if platform . system () == "Windows" else "-j2"
14581 ], cwd = build_dir )
14682
14783 # Copy over the important bits
0 commit comments