|
26 | 26 | """ |
27 | 27 | Verify merging with MergeFlags to CPPPDEFINES with various data types. |
28 | 28 |
|
29 | | -Uses system pkg-config with a stub pc file to generate the data. |
30 | | -Not entirely sure the motivation for this - it would be more "isolated" |
31 | | -to just paste in the data and not depend on pkg-config, but presumably |
32 | | -this test exists because of observed bugs, so leaving alone for now. |
33 | | -
|
34 | | -Ran into an interesting problem when we spun up a GitHub Windows Action: |
35 | | -it finds pre-installed Strawberry Perl's pkg-config over the mingw one, |
36 | | -which is also pre-installed. The Strawberry one is written in pure |
37 | | -Perl (after getting past the initial batch file), so requires perl.exe, |
38 | | -which won't be in the SCons env['ENV']['PATH']. We try to detect it, |
39 | | -by somewhat hokey means: if pkg-config's path had "strawberry' in it, |
40 | | -assume we need to add the directory path to our execution env. |
41 | | -
|
42 | | -It ended up being more portable to use pkg-config's --with-path than |
43 | | -to set PKG_CONFIG_PATH, because the Windows cmd.exe can't accept |
44 | | -the standard calling style. |
| 29 | +Now uses a mocked pkg-config to avoid some weird problems, |
| 30 | +but is still a "live" test because we do expansion on $_CPPDEFFLAGS, |
| 31 | +which needs to have been set up with a compiler tool so have something |
| 32 | +to subst into. |
45 | 33 | """ |
46 | 34 |
|
47 | 35 | import pathlib |
| 36 | +import sys |
48 | 37 |
|
49 | 38 | import TestSCons |
50 | 39 | import TestCmd |
51 | 40 |
|
52 | 41 | test = TestSCons.TestSCons() |
| 42 | +_python_ = TestSCons._python_ |
53 | 43 |
|
54 | | -pkg_config_path = test.where_is('pkg-config') |
55 | | -if not pkg_config_path: |
56 | | - test.skip_test("Could not find 'pkg-config' in PATH, skipping test.\n") |
57 | | -pkg_config_path = pkg_config_path.replace("\\", "/") |
58 | | - |
59 | | -# Try to guess if this was Strawberry Perl's pkg-config |
60 | | -if 'strawberry' in pkg_config_path.lower(): |
61 | | - # as_posix() or the pasted paths in SConstruct will have escape problems |
62 | | - # (or need to be raw strings) |
63 | | - strawberry_perl_path = pathlib.PurePath(pkg_config_path).parent.as_posix() |
64 | | -else: |
65 | | - strawberry_perl_path = "" |
66 | | - |
67 | | -test.write('bug.pc', """\ |
68 | | -prefix=/usr |
69 | | -exec_prefix=${prefix} |
70 | | -libdir=${exec_prefix}/lib |
71 | | -includedir=${prefix}/include |
72 | | -
|
73 | | -Name: bug |
74 | | -Description: A test case .pc file |
75 | | -Version: 1.2 |
76 | | -Cflags: -DSOMETHING -DVARIABLE=2 |
| 44 | +mock_pkg_config = test.workpath('mock-pkg-config.py') |
| 45 | +test.write(mock_pkg_config, f"""\ |
| 46 | +import sys |
| 47 | +if '--cflags' in sys.argv: |
| 48 | + print("-DSOMETHING -DVARIABLE=2") |
| 49 | +sys.exit(0) |
77 | 50 | """) |
78 | 51 |
|
79 | | -test.write('main.c', """\ |
80 | | -int main(int argc, char *argv[]) |
81 | | -{ |
82 | | - return 0; |
83 | | -} |
84 | | -""") |
| 52 | +# Since the mock pkg-config is a Python script, use the selected Python to |
| 53 | +# run it to avoid execution problems (esp. on Windows) |
| 54 | +pc_path = (_python_ + ' ' + mock_pkg_config).replace("\\", "/") |
85 | 55 |
|
| 56 | +# We need a toolset: |
86 | 57 | if TestCmd.IS_WINDOWS: |
87 | | - pkg_config_file = 'bug' |
88 | | - pkg_config_tools = 'mingw' |
| 58 | + pc_tools = 'mingw' |
89 | 59 | else: |
90 | | - pkg_config_file = 'bug.pc' |
91 | | - pkg_config_tools = 'default' |
92 | | -pkg_config_cl_path = '--with-path=.' |
| 60 | + pc_tools = 'default' |
| 61 | + |
| 62 | +# these are no longer really needed, leave so it looks like a "real" pkg-config |
| 63 | +pc_file = 'bug' |
| 64 | +pc_cl_path = "" |
93 | 65 |
|
94 | 66 | test.write('SConstruct', f"""\ |
95 | 67 | import os |
|
98 | 70 | DefaultEnvironment(tools=[]) |
99 | 71 | # https://github.com/SCons/scons/issues/2671 |
100 | 72 | # Passing test cases |
101 | | -env_1 = Environment( |
102 | | - CPPDEFINES=[('DEBUG', '1'), 'TEST'], |
103 | | - tools=['{pkg_config_tools}'] |
104 | | -) |
105 | | -if '{strawberry_perl_path}': |
106 | | - env_1.AppendENVPath('PATH', '{strawberry_perl_path}') |
107 | | -if sys.platform == 'win32': |
108 | | - os.environ['PKG_CONFIG_PATH'] = env_1.Dir('.').abspath.replace("\\\\", "/") |
109 | | -env_1.ParseConfig('"{pkg_config_path}" {pkg_config_cl_path} --cflags {pkg_config_file}') |
| 73 | +env_1 = Environment(CPPDEFINES=[('DEBUG', '1'), 'TEST'], tools=['{pc_tools}']) |
| 74 | +env_1.ParseConfig('{pc_path} {pc_cl_path} --cflags {pc_file}') |
110 | 75 | print(env_1.subst('$_CPPDEFFLAGS')) |
111 | 76 |
|
112 | | -env_2 = Environment( |
113 | | - CPPDEFINES=[('DEBUG', '1'), 'TEST'], |
114 | | - tools=['{pkg_config_tools}'] |
115 | | -) |
116 | | -if '{strawberry_perl_path}': |
117 | | - env_2.AppendENVPath('PATH', '{strawberry_perl_path}') |
| 77 | +env_2 = Environment(CPPDEFINES=[('DEBUG', '1'), 'TEST'], tools=['{pc_tools}']) |
118 | 78 | env_2.MergeFlags('-DSOMETHING -DVARIABLE=2') |
119 | 79 | print(env_2.subst('$_CPPDEFFLAGS')) |
120 | 80 |
|
121 | 81 | # Failing test cases |
122 | | -env_3 = Environment( |
123 | | - CPPDEFINES=dict([('DEBUG', 1), ('TEST', None)]), |
124 | | - tools=['{pkg_config_tools}'], |
125 | | -) |
126 | | -if '{strawberry_perl_path}': |
127 | | - env_3.AppendENVPath('PATH', '{strawberry_perl_path}') |
128 | | -env_3.ParseConfig('"{pkg_config_path}" {pkg_config_cl_path} --cflags {pkg_config_file}') |
| 82 | +env_3 = Environment(CPPDEFINES=dict([('DEBUG', 1), ('TEST', None)]), tools=['{pc_tools}']) |
| 83 | +env_3.ParseConfig('{pc_path} {pc_cl_path} --cflags {pc_file}') |
129 | 84 | print(env_3.subst('$_CPPDEFFLAGS')) |
130 | 85 |
|
131 | | -env_4 = Environment( |
132 | | - CPPDEFINES=dict([('DEBUG', 1), ('TEST', None)]), |
133 | | - tools=['{pkg_config_tools}'], |
134 | | -) |
135 | | -if '{strawberry_perl_path}': |
136 | | - env_4.AppendENVPath('PATH', '{strawberry_perl_path}') |
| 86 | +env_4 = Environment(CPPDEFINES=dict([('DEBUG', 1), ('TEST', None)]), tools=['{pc_tools}']) |
137 | 87 | env_4.MergeFlags('-DSOMETHING -DVARIABLE=2') |
138 | 88 | print(env_4.subst('$_CPPDEFFLAGS')) |
139 | 89 |
|
140 | 90 | # https://github.com/SCons/scons/issues/1738 |
141 | | -# TODO: the Perl pkg-config doesn't work right with both --cflags --libs |
142 | | -# e.g.: pkg-config --with-path=. --libs --cflags bug |
143 | | -# Exepct: -DSOMETHING -DVARIABLE=2 |
144 | | -# Strawberry: (blank) |
145 | | -# We don't have any libs in the stub pc file, so just drop that for now. |
146 | | -env_1738_1 = Environment(tools=['{pkg_config_tools}']) |
147 | | -if '{strawberry_perl_path}': |
148 | | - env_1738_1.AppendENVPath('PATH', '{strawberry_perl_path}') |
149 | | -env_1738_1.ParseConfig( |
150 | | - '"{pkg_config_path}" {pkg_config_cl_path} --cflags {pkg_config_file}' |
151 | | -) |
| 91 | +env_1738_1 = Environment(tools=['{pc_tools}']) |
| 92 | +env_1738_1.ParseConfig('{pc_path} {pc_cl_path} --cflags {pc_file}') |
152 | 93 | env_1738_1.Append(CPPDEFINES={{'value': '1'}}) |
153 | 94 | print(env_1738_1.subst('$_CPPDEFFLAGS')) |
154 | 95 | """) |
|
0 commit comments