19
19
BuildType = Literal ["debug" , "release" ]
20
20
CompilerToolchain = Literal ["gcc" , "clang" , "msvc" ]
21
21
Language = Literal ["c" , "c++" ]
22
+ Binding = Literal ["cpython" , "pybind11" , "nanobind" ]
22
23
Platform = Literal ["linux" , "darwin" , "win32" ]
23
24
PlatformDefaults = {
24
25
"linux" : {"CC" : "gcc" , "CXX" : "g++" , "LD" : "ld" },
@@ -33,12 +34,18 @@ class HatchCppLibrary(BaseModel):
33
34
name : str
34
35
sources : List [str ]
35
36
language : Language = "c++"
37
+
38
+ binding : Binding = "cpython"
39
+ std : Optional [str ] = None
40
+
36
41
include_dirs : List [str ] = Field (default_factory = list , alias = "include-dirs" )
37
42
library_dirs : List [str ] = Field (default_factory = list , alias = "library-dirs" )
38
43
libraries : List [str ] = Field (default_factory = list )
44
+
39
45
extra_compile_args : List [str ] = Field (default_factory = list , alias = "extra-compile-args" )
40
46
extra_link_args : List [str ] = Field (default_factory = list , alias = "extra-link-args" )
41
47
extra_objects : List [str ] = Field (default_factory = list , alias = "extra-objects" )
48
+
42
49
define_macros : List [str ] = Field (default_factory = list , alias = "define-macros" )
43
50
undef_macros : List [str ] = Field (default_factory = list , alias = "undef-macros" )
44
51
@@ -82,29 +89,51 @@ def default() -> HatchCppPlatform:
82
89
83
90
def get_compile_flags (self , library : HatchCppLibrary , build_type : BuildType = "release" ) -> str :
84
91
flags = ""
92
+
93
+ # Python.h
94
+ library .include_dirs .append (get_path ("include" ))
95
+
96
+ if library .binding == "pybind11" :
97
+ import pybind11
98
+
99
+ library .include_dirs .append (pybind11 .get_include ())
100
+ if not library .std :
101
+ library .std = "c++11"
102
+ elif library .binding == "nanobind" :
103
+ import nanobind
104
+
105
+ library .include_dirs .append (nanobind .include_dir ())
106
+ if not library .std :
107
+ library .std = "c++17"
108
+ library .sources .append (str (Path (nanobind .include_dir ()).parent / "src" / "nb_combined.cpp" ))
109
+ library .include_dirs .append (str ((Path (nanobind .include_dir ()).parent / "ext" / "robin_map" / "include" )))
110
+
85
111
if self .toolchain == "gcc" :
86
- flags = f"-I{ get_path ('include' )} "
87
112
flags += " " + " " .join (f"-I{ d } " for d in library .include_dirs )
88
113
flags += " -fPIC"
89
114
flags += " " + " " .join (library .extra_compile_args )
90
115
flags += " " + " " .join (f"-D{ macro } " for macro in library .define_macros )
91
116
flags += " " + " " .join (f"-U{ macro } " for macro in library .undef_macros )
117
+ if library .std :
118
+ flags += f" -std={ library .std } "
92
119
elif self .toolchain == "clang" :
93
- flags = f"-I{ get_path ('include' )} "
94
120
flags += " " .join (f"-I{ d } " for d in library .include_dirs )
95
121
flags += " -fPIC"
96
122
flags += " " + " " .join (library .extra_compile_args )
97
123
flags += " " + " " .join (f"-D{ macro } " for macro in library .define_macros )
98
124
flags += " " + " " .join (f"-U{ macro } " for macro in library .undef_macros )
125
+ if library .std :
126
+ flags += f" -std={ library .std } "
99
127
elif self .toolchain == "msvc" :
100
- flags = f"/I{ get_path ('include' )} "
101
128
flags += " " .join (f"/I{ d } " for d in library .include_dirs )
102
129
flags += " " + " " .join (library .extra_compile_args )
103
130
flags += " " + " " .join (library .extra_link_args )
104
131
flags += " " + " " .join (library .extra_objects )
105
132
flags += " " + " " .join (f"/D{ macro } " for macro in library .define_macros )
106
133
flags += " " + " " .join (f"/U{ macro } " for macro in library .undef_macros )
107
134
flags += " /EHsc /DWIN32"
135
+ if library .std :
136
+ flags += f" /std:{ library .std } "
108
137
# clean
109
138
while flags .count (" " ):
110
139
flags = flags .replace (" " , " " )
@@ -142,7 +171,6 @@ def get_link_flags(self, library: HatchCppLibrary, build_type: BuildType = "rele
142
171
flags += " " + " " .join (library .extra_link_args )
143
172
flags += " " + " " .join (library .extra_objects )
144
173
flags += " /LD"
145
- flags += f" /Fo:{ library .name } .obj"
146
174
flags += f" /Fe:{ library .name } .pyd"
147
175
flags += " /link /DLL"
148
176
if (Path (executable ).parent / "libs" ).exists ():
@@ -178,10 +206,8 @@ def execute(self):
178
206
179
207
def cleanup (self ):
180
208
if self .platform .platform == "win32" :
181
- for library in self .libraries :
182
- temp_obj = Path (f"{ library .name } .obj" )
183
- if temp_obj .exists ():
184
- temp_obj .unlink ()
209
+ for temp_obj in Path ("." ).glob ("*.obj" ):
210
+ temp_obj .unlink ()
185
211
186
212
187
213
class HatchCppBuildConfig (BaseModel ):
0 commit comments