2
2
3
3
from dataclasses import dataclass , field
4
4
from os import environ , system
5
- from sys import platform as sys_platform
5
+ from pathlib import Path
6
+ from sys import executable , platform as sys_platform
6
7
from sysconfig import get_path
7
8
from typing import Literal
8
9
@@ -80,7 +81,7 @@ def default() -> HatchCppPlatform:
80
81
raise Exception (f"Unrecognized toolchain: { CC } , { CXX } " )
81
82
return HatchCppPlatform (cc = CC , cxx = CXX , platform = platform , toolchain = toolchain )
82
83
83
- def get_flags (self , library : HatchCppLibrary ) -> str :
84
+ def get_compile_flags (self , library : HatchCppLibrary ) -> str :
84
85
flags = ""
85
86
if self .toolchain == "gcc" :
86
87
flags = f"-I{ get_path ('include' )} "
@@ -109,20 +110,28 @@ def get_flags(self, library: HatchCppLibrary) -> str:
109
110
elif self .toolchain == "msvc" :
110
111
flags = f"/I{ get_path ('include' )} "
111
112
flags += " " .join (f"/I{ d } " for d in library .include_dirs )
112
- flags += " /LD"
113
113
flags += " " + " " .join (library .extra_compile_args )
114
114
flags += " " + " " .join (library .extra_link_args )
115
115
flags += " " + " " .join (library .extra_objects )
116
- flags += " " + " " .join (f"{ lib } .lib" for lib in library .libraries )
117
- flags += " " + " " .join (f"/LIBPATH:{ lib } " for lib in library .library_dirs )
118
116
flags += " " + " " .join (f"/D{ macro } " for macro in library .define_macros )
119
117
flags += " " + " " .join (f"/U{ macro } " for macro in library .undef_macros )
120
- flags += f" /Fo{ library .name } .pyd"
118
+ flags += " /EHsc /DWIN32 /LD"
119
+ flags += f" /Fo:{ library .name } .obj"
120
+ flags += f" /Fe:{ library .name } .pyd"
121
+ flags += " /link /DLL"
122
+ if (Path (executable ).parent / "libs" ).exists ():
123
+ flags += f" /LIBPATH:{ str (Path (executable ).parent / 'libs' )} "
124
+ flags += " " + " " .join (f"{ lib } .lib" for lib in library .libraries )
125
+ flags += " " + " " .join (f"/LIBPATH:{ lib } " for lib in library .library_dirs )
121
126
# clean
122
127
while flags .count (" " ):
123
128
flags = flags .replace (" " , " " )
124
129
return flags
125
130
131
+ def get_link_flags (self , library : HatchCppLibrary ) -> str :
132
+ flags = ""
133
+ return flags
134
+
126
135
127
136
@dataclass
128
137
class HatchCppBuildPlan (object ):
@@ -133,11 +142,18 @@ class HatchCppBuildPlan(object):
133
142
def generate (self ):
134
143
self .commands = []
135
144
for library in self .libraries :
136
- flags = self .platform .get_flags (library )
145
+ flags = self .platform .get_compile_flags (library )
137
146
self .commands .append (f"{ self .platform .cc } { ' ' .join (library .sources )} { flags } " )
138
147
return self .commands
139
148
140
149
def execute (self ):
141
150
for command in self .commands :
142
151
system (command )
143
152
return self .commands
153
+
154
+ def cleanup (self ):
155
+ if self .platform .platform == "win32" :
156
+ for library in self .libraries :
157
+ temp_obj = Path (f"{ library .name } .obj" )
158
+ if temp_obj .exists ():
159
+ temp_obj .unlink ()
0 commit comments