22
33from dataclasses import dataclass , field
44from 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
67from sysconfig import get_path
78from typing import Literal
89
@@ -80,7 +81,7 @@ def default() -> HatchCppPlatform:
8081 raise Exception (f"Unrecognized toolchain: { CC } , { CXX } " )
8182 return HatchCppPlatform (cc = CC , cxx = CXX , platform = platform , toolchain = toolchain )
8283
83- def get_flags (self , library : HatchCppLibrary ) -> str :
84+ def get_compile_flags (self , library : HatchCppLibrary ) -> str :
8485 flags = ""
8586 if self .toolchain == "gcc" :
8687 flags = f"-I{ get_path ('include' )} "
@@ -109,20 +110,28 @@ def get_flags(self, library: HatchCppLibrary) -> str:
109110 elif self .toolchain == "msvc" :
110111 flags = f"/I{ get_path ('include' )} "
111112 flags += " " .join (f"/I{ d } " for d in library .include_dirs )
112- flags += " /LD"
113113 flags += " " + " " .join (library .extra_compile_args )
114114 flags += " " + " " .join (library .extra_link_args )
115115 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 )
118116 flags += " " + " " .join (f"/D{ macro } " for macro in library .define_macros )
119117 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 )
121126 # clean
122127 while flags .count (" " ):
123128 flags = flags .replace (" " , " " )
124129 return flags
125130
131+ def get_link_flags (self , library : HatchCppLibrary ) -> str :
132+ flags = ""
133+ return flags
134+
126135
127136@dataclass
128137class HatchCppBuildPlan (object ):
@@ -133,11 +142,18 @@ class HatchCppBuildPlan(object):
133142 def generate (self ):
134143 self .commands = []
135144 for library in self .libraries :
136- flags = self .platform .get_flags (library )
145+ flags = self .platform .get_compile_flags (library )
137146 self .commands .append (f"{ self .platform .cc } { ' ' .join (library .sources )} { flags } " )
138147 return self .commands
139148
140149 def execute (self ):
141150 for command in self .commands :
142151 system (command )
143152 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