1- from os .path import exists
1+ import os
2+ import pathlib
3+ import typing as T
24
3- from .util import get_setup
45from ..autowrap .generator_data import MissingReporter
6+ from ..cmd .header2dat import make_argparser , generate_wrapper
7+ from ..makeplan import InputFile , makeplan , BuildTarget
58
69
710class GenCreator :
@@ -15,35 +18,85 @@ def add_subparser(cls, parent_parser, subparsers):
1518 parser .add_argument (
1619 "--write" , help = "Write to files if they don't exist" , action = "store_true"
1720 )
18- parser .add_argument ("--strip-prefixes" , action = "append" )
1921
2022 return parser
2123
2224 def run (self , args ):
23- pfx = ""
24- if args .strip_prefixes :
25- pfx = "strip_prefixes:\n - " + "\n - " .join (args .strip_prefixes ) + "\n \n "
25+ project_root = pathlib .Path .cwd ()
26+
27+ # Problem: if another hatchling plugin sets PKG_CONFIG_PATH to include a .pc
28+ # file, makeplan() will fail to find it, which prevents a semiwrap program
29+ # from consuming those .pc files.
30+ #
31+ # We search for .pc files in the project root by default and add anything found
32+ # to the PKG_CONFIG_PATH to allow that to work. Probably won't hurt anything?
33+
34+ pcpaths : T .Set [str ] = set ()
35+ for pcfile in project_root .glob ("**/*.pc" ):
36+ pcpaths .add (str (pcfile .parent ))
37+
38+ if pcpaths :
39+ # Add to PKG_CONFIG_PATH so that it can be resolved by other hatchling
40+ # plugins if desired
41+ pkg_config_path = os .environ .get ("PKG_CONFIG_PATH" )
42+ if pkg_config_path is not None :
43+ os .environ ["PKG_CONFIG_PATH" ] = os .pathsep .join (
44+ (pkg_config_path , * pcpaths )
45+ )
46+ else :
47+ os .environ ["PKG_CONFIG_PATH" ] = os .pathsep .join (pcpaths )
48+
49+ plan = makeplan (project_root , missing_yaml_ok = True )
50+
51+ for item in plan :
52+ if not isinstance (item , BuildTarget ) or item .command != "header2dat" :
53+ continue
54+
55+ # convert args to string so we can parse it
56+ # .. this is weird, but less annoying than other alternatives
57+ # that I can think of?
58+ argv = []
59+ for arg in item .args :
60+ if isinstance (arg , str ):
61+ argv .append (arg )
62+ elif isinstance (arg , InputFile ):
63+ argv .append (str (arg .path .absolute ()))
64+ elif isinstance (arg , pathlib .Path ):
65+ argv .append (str (arg .absolute ()))
66+ else :
67+ # anything else shouldn't matter
68+ argv .append ("ignored" )
69+
70+ sparser = make_argparser ()
71+ sargs = sparser .parse_args (argv )
2672
27- s = get_setup ()
28- for wrapper in s .wrappers :
2973 reporter = MissingReporter ()
30- wrapper .on_build_gen ("" , reporter )
31-
32- nada = True
33- for name , report in reporter .as_yaml ():
34- report = f"---\n \n { pfx } { report } "
35-
36- nada = False
37- if args .write :
38- if not exists (name ):
39- print ("Writing" , name )
40- with open (name , "w" ) as fp :
41- fp .write (report )
42- else :
43- print (name , "already exists!" )
44-
45- print ("===" , name , "===" )
46- print (report )
47-
48- if nada :
49- print ("Nothing to do!" )
74+
75+ generate_wrapper (
76+ name = sargs .name ,
77+ src_yml = sargs .src_yml ,
78+ src_h = sargs .src_h ,
79+ src_h_root = sargs .src_h_root ,
80+ dst_dat = None ,
81+ dst_depfile = None ,
82+ include_paths = sargs .include_paths ,
83+ casters = {},
84+ pp_defines = sargs .pp_defines ,
85+ missing_reporter = reporter ,
86+ report_only = True ,
87+ )
88+
89+ if reporter :
90+ for name , report in reporter .as_yaml ():
91+ report = f"---\n \n { report } "
92+
93+ if args .write :
94+ if not name .exists ():
95+ print ("Writing" , name )
96+ with open (name , "w" ) as fp :
97+ fp .write (report )
98+ else :
99+ print (name , "already exists!" )
100+
101+ print ("===" , name , "===" )
102+ print (report )
0 commit comments