Skip to content

Commit 2a85d9e

Browse files
authored
Merge pull request #4676 from rdbisme/extra-libs
🐛 Allow passing `extra_libs` to `CheckLibWithHeader`
2 parents 797ce27 + 769eff9 commit 2a85d9e

File tree

14 files changed

+245
-26
lines changed

14 files changed

+245
-26
lines changed

CHANGES.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ NOTE: Python 3.6 support is deprecated and will be dropped in a future release.
1212

1313
RELEASE VERSION/DATE TO BE FILLED IN LATER
1414

15+
From Ruben Di Battista:
16+
- Expose `extra_libs` kwarg in Configure checks `CheckLibWithHeader`
17+
and 'CheckLib' and forward it downstream to `CheckLib`
18+
1519
From Joseph Brill:
1620
- Added error handling when creating MSVC detection debug log file specified by
1721
SCONS_MSCOMMON_DEBUG.

RELEASE.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ DEPRECATED FUNCTIONALITY
2525

2626
CHANGED/ENHANCED EXISTING FUNCTIONALITY
2727
---------------------------------------
28+
- Expose the `extra_libs` keyword argument in `CheckLibWithHeader` and 'CheckLib'
2829

2930
- List modifications to existing features, where the previous behavior
3031
wouldn't actually be considered a bug

SCons/SConf.py

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1101,12 +1101,17 @@ def CheckCXXHeader(context, header, include_quotes: str = '""'):
11011101

11021102

11031103
def CheckLib(context, library = None, symbol: str = "main",
1104-
header = None, language = None, autoadd: bool=True,
1105-
append: bool=True, unique: bool=False) -> bool:
1104+
header = None, language = None, extra_libs = None,
1105+
autoadd: bool=True, append: bool=True, unique: bool=False) -> bool:
11061106
"""
1107-
A test for a library. See also CheckLibWithHeader.
1107+
A test for a library. See also :func:`CheckLibWithHeader`.
11081108
Note that library may also be None to test whether the given symbol
11091109
compiles without flags.
1110+
1111+
.. versionchanged:: NEXT_RELEASE
1112+
Added the *extra_libs* keyword parameter. The actual implementation
1113+
is in :func:`SCons.Conftest.CheckLib` which already accepted this
1114+
parameter, so this is only exposing existing functionality.
11101115
"""
11111116

11121117
if not library:
@@ -1116,25 +1121,31 @@ def CheckLib(context, library = None, symbol: str = "main",
11161121
library = [library]
11171122

11181123
# ToDo: accept path for the library
1119-
res = SCons.Conftest.CheckLib(context, library, symbol, header = header,
1120-
language = language, autoadd = autoadd,
1121-
append=append, unique=unique)
1124+
res = SCons.Conftest.CheckLib(context, library, symbol, header=header,
1125+
language=language, extra_libs=extra_libs,
1126+
autoadd=autoadd, append=append, unique=unique)
11221127
context.did_show_result = True
11231128
return not res
11241129

11251130
# XXX
11261131
# Bram: Can only include one header and can't use #ifdef HAVE_HEADER_H.
11271132

11281133
def CheckLibWithHeader(context, libs, header, language,
1129-
call = None, autoadd: bool=True, append: bool=True, unique: bool=False) -> bool:
1130-
# ToDo: accept path for library. Support system header files.
1134+
extra_libs = None, call = None, autoadd: bool=True,
1135+
append: bool=True, unique: bool=False) -> bool:
11311136
"""
11321137
Another (more sophisticated) test for a library.
11331138
Checks, if library and header is available for language (may be 'C'
11341139
or 'CXX'). Call maybe be a valid expression _with_ a trailing ';'.
1135-
As in CheckLib, we support library=None, to test if the call compiles
1140+
As in :func:`CheckLib`, we support library=None, to test if the call compiles
11361141
without extra link flags.
1142+
1143+
.. versionchanged:: NEXT_RELEASE
1144+
Added the *extra_libs* keyword parameter. The actual implementation
1145+
is in :func:`SCons.Conftest.CheckLib` which already accepted this
1146+
parameter, so this is only exposing existing functionality.
11371147
"""
1148+
# ToDo: accept path for library. Support system header files.
11381149
prog_prefix, dummy = createIncludesFromHeaders(header, 0)
11391150
if not libs:
11401151
libs = [None]
@@ -1143,8 +1154,8 @@ def CheckLibWithHeader(context, libs, header, language,
11431154
libs = [libs]
11441155

11451156
res = SCons.Conftest.CheckLib(context, libs, None, prog_prefix,
1146-
call = call, language = language, autoadd=autoadd,
1147-
append=append, unique=unique)
1157+
extra_libs = extra_libs, call = call, language = language,
1158+
autoadd=autoadd, append=append, unique=unique)
11481159
context.did_show_result = 1
11491160
return not res
11501161

doc/man/scons.xml

Lines changed: 43 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4163,7 +4163,7 @@ function calls whose arguments are not type compatible with the prototype.
41634163
</varlistentry>
41644164

41654165
<varlistentry>
4166-
<term><replaceable>context</replaceable>.<methodname>CheckLib</methodname>(<parameter>[library, symbol, header, language, autoadd=True, append=True, unique=False]</parameter>) </term>
4166+
<term><replaceable>context</replaceable>.<methodname>CheckLib</methodname>(<parameter>[library, symbol, header, language, extra_libs=None, autoadd=True, append=True, unique=False]</parameter>) </term>
41674167
<listitem>
41684168
<para>Checks if
41694169
<parameter>library</parameter>
@@ -4173,12 +4173,19 @@ with the compiler selected by <parameter>language</parameter>,
41734173
and optionally adds that library to the context.
41744174
If supplied, the text of <parameter>header</parameter> is included at the
41754175
top of the stub.
4176+
</para>
4177+
<para>
4178+
The remaining arguments should be specified in keyword style.
4179+
If <parameter>extra_libs</parameter> is specified,
4180+
it is a list off additional libraries to include when
4181+
linking the stub program (usually, dependencies of
4182+
the library being checked).
41764183
If <parameter>autoadd</parameter> is true (the default),
41774184
and the library provides the specified
4178-
<parameter>symbol</parameter> (as defined by successfully
4179-
linking the stub program),
4185+
<parameter>symbol</parameter>,
4186+
as defined by successfully linking the stub program,
41804187
it is added to the &cv-link-LIBS; &consvar; in the context.
4181-
if <parameter>append</parameter> is true (the default),
4188+
If <parameter>append</parameter> is true (the default),
41824189
the library is appended, otherwise it is prepended.
41834190
If <parameter>unique</parameter> is true,
41844191
and the library would otherwise be added but is
@@ -4212,38 +4219,53 @@ at least one should be supplied.
42124219
<emphasis>Changed in version 4.5.0: added the
42134220
<parameter>append</parameter> and <parameter>unique</parameter>
42144221
parameters.</emphasis>
4222+
</para>
4223+
<para>
4224+
<emphasis>Changed in version NEXT_RELEASE: added the
4225+
<parameter>extra_libs</parameter> parameter.</emphasis>
42154226
</para>
42164227
</listitem>
42174228
</varlistentry>
42184229

42194230
<varlistentry>
4220-
<term><replaceable>context</replaceable>.<methodname>CheckLibWithHeader</methodname>(<parameter>library, header, [language, call, autoadd=True, append=True, unique=False]</parameter>)</term>
4231+
<term><replaceable>context</replaceable>.<methodname>CheckLibWithHeader</methodname>(<parameter>[library, header, language, extra_libs=None, call=None, autoadd=True, append=True, unique=False]</parameter>)</term>
42214232
<listitem>
42224233

42234234
<para>Provides an alternative to the
42244235
<methodname>CheckLib</methodname> method
4225-
for checking for libraries usable in a build.
4236+
for checking whether libraries are usable in a build.
4237+
The first three arguments can be given as
4238+
positional or keyword style arguments.
42264239
<parameter>library</parameter>
4227-
specifies a library or list of libraries to check.
4240+
specifies a library or list of libraries to check
4241+
(the default is <literal>None</literal>),
42284242
<parameter>header</parameter>
4229-
specifies a header to include in the test program,
4230-
and <parameter>language</parameter> indicates the compiler to use.
4243+
specifies header text to include in the test program.
42314244
<parameter>header</parameter>
4232-
may be a list,
4245+
may also be a list,
42334246
in which case the last item in the list
42344247
is the header file to be checked,
42354248
and the previous list items are
42364249
header files whose
42374250
<literal>#include</literal>
42384251
lines should precede the
42394252
header line being checked for.
4240-
A code fragment
4241-
(must be a valid expression, including a trailing semicolon)
4242-
to serve as the test can be supplied in
4243-
<parameter>call</parameter>;
4244-
if not supplied,
4253+
The default is to include no header text.
4254+
<parameter>language</parameter> indicates the compiler to use
4255+
(default "C").
4256+
</para>
4257+
4258+
<para>
4259+
The remaining parameters should be specified in keyword style.
4260+
If provided, <parameter>call</parameter>
4261+
is a code fragment to compile as the stub test,
4262+
replacing the auto-generated stub.
4263+
The fragment must be a valid expression in <parameter>language</parameter>.
4264+
If not supplied,
42454265
the default checks the ability to link against the specified
42464266
<parameter>library</parameter>.
4267+
<parameter>extra_libs</parameter> can be used to add additional libraries
4268+
to link against (usually, dependencies of the library under test).
42474269
If <parameter>autoadd</parameter> is true (the default),
42484270
the first library that passes the check
42494271
is added to the &cv-link-LIBS; &consvar; in the context
@@ -4255,11 +4277,17 @@ and the library would otherwise be added but is
42554277
already present in &cv-link-LIBS; in the configure context,
42564278
it will not be added again. The default is <literal>False</literal>.
42574279
</para>
4280+
42584281
<para>Returns a boolean indicating success or failure.</para>
4282+
42594283
<para>
42604284
<emphasis>Changed in version 4.5.0: added the
42614285
<parameter>append</parameter> and <parameter>unique</parameter>
42624286
parameters.</emphasis>
4287+
</para>
4288+
<para>
4289+
<emphasis>Changed in version NEXT_RELEASE: added the
4290+
<parameter>extra_libs</parameter> parameter.</emphasis>
42634291
</para>
42644292
</listitem>
42654293
</varlistentry>
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
#!/usr/bin/env python
2+
#
3+
# MIT License
4+
#
5+
# Copyright The SCons Foundation
6+
#
7+
# Permission is hereby granted, free of charge, to any person obtaining
8+
# a copy of this software and associated documentation files (the
9+
# "Software"), to deal in the Software without restriction, including
10+
# without limitation the rights to use, copy, modify, merge, publish,
11+
# distribute, sublicense, and/or sell copies of the Software, and to
12+
# permit persons to whom the Software is furnished to do so, subject to
13+
# the following conditions:
14+
#
15+
# The above copyright notice and this permission notice shall be included
16+
# in all copies or substantial portions of the Software.
17+
#
18+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
19+
# KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
20+
# WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21+
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
22+
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
23+
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
24+
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE
25+
26+
"""
27+
Verify that a program which depends on library which in turn depends
28+
on another library can be built correctly using CheckLibWithHeader
29+
30+
This is a "live" test - requires a configured C compiler/toolchain to run.
31+
"""
32+
33+
from TestSCons import TestSCons, dll_, _dll
34+
35+
test = TestSCons(match=TestSCons.match_re_dotall)
36+
test.dir_fixture(['fixture', 'checklib_extra'])
37+
38+
libA = f"libA/{dll_}A{_dll}"
39+
libB = f"libB/{dll_}B{_dll}"
40+
41+
test.run(arguments='-C libA')
42+
test.must_exist(libA)
43+
test.run(arguments='-C libB')
44+
test.must_exist(libB)
45+
46+
test.run()
47+
48+
test.pass_test()
49+
50+
# Local Variables:
51+
# tab-width:4
52+
# indent-tabs-mode:nil
53+
# End:
54+
# vim: set expandtab tabstop=4 shiftwidth=4:
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# SPDX-License-Identifier: MIT
2+
#
3+
# Copyright The SCons Foundation
4+
DefaultEnvironment(tools=[])
5+
6+
env = Environment(
7+
CPPPATH=['#'],
8+
LIBPATH=['libB', 'libA'],
9+
LIBS=['A', 'B'],
10+
RPATH=['libA', 'libB'],
11+
)
12+
13+
conf = Configure(env)
14+
if not conf.CheckLibWithHeader(
15+
['B'],
16+
header="libB/libB.h",
17+
language='C',
18+
extra_libs=['A'],
19+
call='libB();',
20+
autoadd=False,
21+
):
22+
print("Cannot build against 'B' library, exiting.")
23+
Exit(1)
24+
env = conf.Finish()
25+
26+
# TODO: we should be able to build and run a test program now,
27+
# to make sure Configure() didn't lie to us about usability.
28+
# Disabled for now, because that's trickier in Windows (the rpath
29+
# only works for Linux)
30+
# env.Program(target="testlibs", source="src/test.c")
31+

test/Configure/fixture/checklib_extra/conftest.skip

Whitespace-only changes.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# SPDX-License-Identifier: MIT
2+
#
3+
# Copyright The SCons Foundation
4+
5+
SharedLibrary(target='A', source=['libA.c'], CPPDEFINES='BUILDINGSHAREDLIB')
6+
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// SPDX-License-Identifier: MIT
2+
//
3+
// Copyright The SCons Foundation
4+
5+
#include <stdio.h>
6+
#include "libA.h"
7+
8+
LIBA_DECL void libA(void) {
9+
printf("libA\\n");
10+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// SPDX-License-Identifier: MIT
2+
//
3+
// Copyright The SCons Foundation
4+
5+
#ifndef _LIBA_H
6+
#define _LIBA_H
7+
8+
// define BUILDINGSHAREDLIB when building libA as shared lib
9+
#ifdef _MSC_VER
10+
# ifdef BUILDINGSHAREDLIB
11+
# define LIBA_DECL __declspec(dllexport)
12+
# else
13+
# define LIBA_DECL __declspec(dllimport)
14+
# endif
15+
#endif // WIN32
16+
17+
#ifndef LIBA_DECL
18+
# define LIBA_DECL
19+
#endif
20+
21+
LIBA_DECL void libA(void);
22+
#endif // _LIBA_H

0 commit comments

Comments
 (0)