11from __future__ import annotations
22
3+ from logging import getLogger
34from os import system as system_call
45from pathlib import Path
56from typing import List , Optional
67
78from pydantic import BaseModel , Field , model_validator
89
9- from .toolchains import BuildType , HatchCppCmakeConfiguration , HatchCppLibrary , HatchCppPlatform , HatchCppVcpkgConfiguration
10+ from .toolchains import BuildType , HatchCppCmakeConfiguration , HatchCppLibrary , HatchCppPlatform , HatchCppVcpkgConfiguration , Toolchain
1011
1112__all__ = (
1213 "HatchCppBuildConfig" ,
1314 "HatchCppBuildPlan" ,
1415)
1516
1617
18+ _log = getLogger (__name__ )
19+
20+
1721class HatchCppBuildConfig (BaseModel ):
1822 """Build config values for Hatch C++ Builder."""
1923
@@ -22,7 +26,7 @@ class HatchCppBuildConfig(BaseModel):
2226 libraries : List [HatchCppLibrary ] = Field (default_factory = list )
2327 cmake : Optional [HatchCppCmakeConfiguration ] = Field (default = None )
2428 platform : Optional [HatchCppPlatform ] = Field (default_factory = HatchCppPlatform .default )
25- vcpkg : Optional [HatchCppVcpkgConfiguration ] = Field (default = None )
29+ vcpkg : Optional [HatchCppVcpkgConfiguration ] = Field (default_factory = HatchCppVcpkgConfiguration )
2630
2731 @model_validator (mode = "wrap" )
2832 @classmethod
@@ -41,6 +45,8 @@ def validate_model(cls, data, handler):
4145 if "ld" in data :
4246 data ["platform" ].ld = data ["ld" ]
4347 data .pop ("ld" )
48+ if "vcpkg" in data and data ["vcpkg" ] == "false" :
49+ data ["vcpkg" ] = None
4450 model = handler (data )
4551 if model .cmake and model .libraries :
4652 raise ValueError ("Must not provide libraries when using cmake toolchain." )
@@ -51,20 +57,35 @@ class HatchCppBuildPlan(HatchCppBuildConfig):
5157 build_type : BuildType = "release"
5258 commands : List [str ] = Field (default_factory = list )
5359
60+ _active_toolchains : List [Toolchain ] = []
61+
5462 def generate (self ):
5563 self .commands = []
5664
65+ # Evaluate toolchains
5766 if self .vcpkg and Path (self .vcpkg .vcpkg ).exists ():
58- self .commands .extend (self .vcpkg .generate (self .platform ))
59-
67+ self ._active_toolchains .append ("vcpkg" )
6068 if self .libraries :
69+ self ._active_toolchains .append ("vanilla" )
70+ elif self .cmake :
71+ self ._active_toolchains .append ("cmake" )
72+
73+ # Collect toolchain commands
74+ if "vcpkg" in self ._active_toolchains :
75+ self .commands .extend (self .vcpkg .generate (self ))
76+
77+ if "vanilla" in self ._active_toolchains :
78+ if "vcpkg" in self ._active_toolchains :
79+ _log .warning ("vcpkg toolchain is active; ensure that your compiler is configured to use vcpkg includes and libs." )
80+
6181 for library in self .libraries :
6282 compile_flags = self .platform .get_compile_flags (library , self .build_type )
6383 link_flags = self .platform .get_link_flags (library , self .build_type )
6484 self .commands .append (
6585 f"{ self .platform .cc if library .language == 'c' else self .platform .cxx } { ' ' .join (library .sources )} { compile_flags } { link_flags } "
6686 )
67- elif self .cmake :
87+
88+ if "cmake" in self ._active_toolchains :
6889 self .commands .extend (self .cmake .generate (self ))
6990
7091 return self .commands
0 commit comments