1919BuildType = Literal ["debug" , "release" ]
2020CompilerToolchain = Literal ["gcc" , "clang" , "msvc" ]
2121Language = Literal ["c" , "c++" ]
22+ Binding = Literal ["cpython" , "pybind11" , "nanobind" ]
2223Platform = Literal ["linux" , "darwin" , "win32" ]
2324PlatformDefaults = {
2425 "linux" : {"CC" : "gcc" , "CXX" : "g++" , "LD" : "ld" },
@@ -33,12 +34,18 @@ class HatchCppLibrary(BaseModel):
3334 name : str
3435 sources : List [str ]
3536 language : Language = "c++"
37+
38+ binding : Binding = "cpython"
39+ std : Optional [str ] = None
40+
3641 include_dirs : List [str ] = Field (default_factory = list , alias = "include-dirs" )
3742 library_dirs : List [str ] = Field (default_factory = list , alias = "library-dirs" )
3843 libraries : List [str ] = Field (default_factory = list )
44+
3945 extra_compile_args : List [str ] = Field (default_factory = list , alias = "extra-compile-args" )
4046 extra_link_args : List [str ] = Field (default_factory = list , alias = "extra-link-args" )
4147 extra_objects : List [str ] = Field (default_factory = list , alias = "extra-objects" )
48+
4249 define_macros : List [str ] = Field (default_factory = list , alias = "define-macros" )
4350 undef_macros : List [str ] = Field (default_factory = list , alias = "undef-macros" )
4451
@@ -82,29 +89,51 @@ def default() -> HatchCppPlatform:
8289
8390 def get_compile_flags (self , library : HatchCppLibrary , build_type : BuildType = "release" ) -> str :
8491 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+
85111 if self .toolchain == "gcc" :
86- flags = f"-I{ get_path ('include' )} "
87112 flags += " " + " " .join (f"-I{ d } " for d in library .include_dirs )
88113 flags += " -fPIC"
89114 flags += " " + " " .join (library .extra_compile_args )
90115 flags += " " + " " .join (f"-D{ macro } " for macro in library .define_macros )
91116 flags += " " + " " .join (f"-U{ macro } " for macro in library .undef_macros )
117+ if library .std :
118+ flags += f" -std={ library .std } "
92119 elif self .toolchain == "clang" :
93- flags = f"-I{ get_path ('include' )} "
94120 flags += " " .join (f"-I{ d } " for d in library .include_dirs )
95121 flags += " -fPIC"
96122 flags += " " + " " .join (library .extra_compile_args )
97123 flags += " " + " " .join (f"-D{ macro } " for macro in library .define_macros )
98124 flags += " " + " " .join (f"-U{ macro } " for macro in library .undef_macros )
125+ if library .std :
126+ flags += f" -std={ library .std } "
99127 elif self .toolchain == "msvc" :
100- flags = f"/I{ get_path ('include' )} "
101128 flags += " " .join (f"/I{ d } " for d in library .include_dirs )
102129 flags += " " + " " .join (library .extra_compile_args )
103130 flags += " " + " " .join (library .extra_link_args )
104131 flags += " " + " " .join (library .extra_objects )
105132 flags += " " + " " .join (f"/D{ macro } " for macro in library .define_macros )
106133 flags += " " + " " .join (f"/U{ macro } " for macro in library .undef_macros )
107134 flags += " /EHsc /DWIN32"
135+ if library .std :
136+ flags += f" /std:{ library .std } "
108137 # clean
109138 while flags .count (" " ):
110139 flags = flags .replace (" " , " " )
@@ -142,7 +171,6 @@ def get_link_flags(self, library: HatchCppLibrary, build_type: BuildType = "rele
142171 flags += " " + " " .join (library .extra_link_args )
143172 flags += " " + " " .join (library .extra_objects )
144173 flags += " /LD"
145- flags += f" /Fo:{ library .name } .obj"
146174 flags += f" /Fe:{ library .name } .pyd"
147175 flags += " /link /DLL"
148176 if (Path (executable ).parent / "libs" ).exists ():
@@ -178,10 +206,8 @@ def execute(self):
178206
179207 def cleanup (self ):
180208 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 ()
185211
186212
187213class HatchCppBuildConfig (BaseModel ):
0 commit comments