Skip to content

Commit c069c94

Browse files
N-R-Kskeeto
authored andcommitted
Don't print system paths by default
mainly for compatibility with pkgconf. also the `--keep-system-libs` flag seems to be used in practice [0] so seems to be worth it to support it. [0]: https://github.com/gentoo/gentoo/blob/b743fa0be223a9a220cae24820048d53b7ee0b31/dev-lang/python/python-3.10.9.ebuild#L234
1 parent b7fafa6 commit c069c94

3 files changed

Lines changed: 59 additions & 3 deletions

File tree

README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ summary:
2727

2828
* Omits most `.pc` debugging features (`--print-…`)
2929
* No special handling of "uninstalled" packages, and no attendant knobs
30-
* No removal of standard system paths (e.g. `/usr/include`)
3130
* Skips checks unimplemented by pkg-config (i.e. `Conflicts:`)
3231
* Omits clunky redundant features (`--exists`, `--errors-to-stdout`, etc.)
3332
* Less strict `.pc` syntax

generic_main.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,14 @@
1818
# endif
1919
#endif
2020

21+
#ifndef PKG_CONFIG_SYSTEM_INCLUDE_PATH
22+
# define PKG_CONFIG_SYSTEM_INCLUDE_PATH "/usr/include"
23+
#endif
24+
25+
#ifndef PKG_CONFIG_SYSTEM_LIBRARY_PATH
26+
# define PKG_CONFIG_SYSTEM_LIBRARY_PATH "/lib" PATHDELIM "/usr/lib"
27+
#endif
28+
2129
#ifndef PKG_CONFIG_PREFIX
2230
# define PKG_CONFIG_PREFIX "/usr"
2331
#endif
@@ -85,6 +93,14 @@ int main(int argc, char **argv)
8593
if (!conf.fixedpath.s) {
8694
conf.fixedpath = S(pkg_config_path);
8795
}
96+
conf.sys_incpath = fromcstr_(getenv("PKG_CONFIG_SYSTEM_INCLUDE_PATH"));
97+
if (!conf.sys_incpath.s) {
98+
conf.sys_incpath = S(PKG_CONFIG_SYSTEM_INCLUDE_PATH);
99+
}
100+
conf.sys_libpath = fromcstr_(getenv("PKG_CONFIG_SYSTEM_LIBRARY_PATH"));
101+
if (!conf.sys_libpath.s) {
102+
conf.sys_libpath = S(PKG_CONFIG_SYSTEM_LIBRARY_PATH);
103+
}
88104
conf.top_builddir = fromcstr_(getenv("PKG_CONFIG_TOP_BUILD_DIR"));
89105

90106
appmain(conf);

u-config.c

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ typedef struct {
5151
Str envpath; // $PKG_CONFIG_PATH or empty
5252
Str fixedpath; // $PKG_CONFIG_LIBDIR or default
5353
Str top_builddir; // $PKG_CONFIG_TOP_BUILD_DIR or default
54+
Str sys_incpath; // $PKG_CONFIG_SYSTEM_INCLUDE_PATH or default
55+
Str sys_libpath; // $PKG_CONFIG_SYSTEM_LIBRARY_PATH or default
5456
Bool define_prefix;
5557
Byte delim;
5658
} Config;
@@ -805,6 +807,7 @@ static void usage(Out *out)
805807
" --cflags, --cflags-only-I, --cflags-only-other\n"
806808
" --define-prefix, --dont-define-prefix\n"
807809
" --define-variable=NAME=VALUE, --variable=NAME\n"
810+
" --keep-system-cflags, --keep-system-libs\n"
808811
" --libs, --libs-only-L, --libs-only-l, --libs-only-other\n"
809812
" --modversion\n"
810813
" --newlines\n"
@@ -1539,6 +1542,32 @@ static OutConfig newoutconf(Arena *a, Out *out, Out *err)
15391542
return r;
15401543
}
15411544

1545+
static void insertsyspath(OutConfig *conf, Str path, Byte delim, Byte flag)
1546+
{
1547+
Byte flagbuf[] = {'-', flag};
1548+
Str prefix = {flagbuf, SIZEOF(flagbuf)};
1549+
while (path.len) {
1550+
Arena snapshot = *conf->arena;
1551+
Cut c = cut(path, delim);
1552+
Str dir = c.head;
1553+
path = c.tail;
1554+
if (!dir.len) {
1555+
continue;
1556+
}
1557+
Str syspath = newstr(&snapshot, prefix.len+dir.len);
1558+
copy(copy(syspath, prefix), dir);
1559+
// NOTE(NRK): Technically, the path doesn't need to follow the flag
1560+
// immediately (e.g `-I /usr/include`). But I have not seen a single
1561+
// pc file that does this.
1562+
//
1563+
// In fact, `pkgconf` also fails to recognize `-I /usr/include` as
1564+
// a system include path! So this should be fine in practice.
1565+
if (insertstr(&snapshot, conf->seen, syspath)) {
1566+
*conf->arena = snapshot;
1567+
}
1568+
}
1569+
}
1570+
15421571
// Process the field while writing it to the output.
15431572
static void fieldout(OutConfig *conf, Pkg *p, Str field)
15441573
{
@@ -1589,6 +1618,8 @@ static void appmain(Config conf)
15891618
Bool silent = 0;
15901619
Bool static_ = 0;
15911620
Bool modversion = 0;
1621+
Bool print_sysinc = 0;
1622+
Bool print_syslib = 0;
15921623
Str variable = {0, 0};
15931624

15941625
proc.define_prefix = conf.define_prefix;
@@ -1597,6 +1628,8 @@ static void appmain(Config conf)
15971628
}
15981629

15991630
*insert(a, &global, S("pc_path")) = conf.fixedpath;
1631+
*insert(a, &global, S("pc_system_includedirs")) = conf.sys_incpath;
1632+
*insert(a, &global, S("pc_system_libdirs")) = conf.sys_libpath;
16001633
*insert(a, &global, S("pc_sysrootdir")) = S("/");
16011634
*insert(a, &global, S("pc_top_builddir")) = conf.top_builddir;
16021635

@@ -1707,10 +1740,10 @@ static void appmain(Config conf)
17071740
// Ignore
17081741

17091742
} else if (equals(r.arg, S("-keep-system-cflags"))) {
1710-
// Ignore: already the default behavior
1743+
print_sysinc = 1;
17111744

17121745
} else if (equals(r.arg, S("-keep-system-libs"))) {
1713-
// Ignore: already the default behavior
1746+
print_syslib = 1;
17141747

17151748
} else if (equals(r.arg, S("-validate"))) {
17161749
silent = 1;
@@ -1730,6 +1763,14 @@ static void appmain(Config conf)
17301763
err = newnullout();
17311764
}
17321765

1766+
if (!print_sysinc) {
1767+
insertsyspath(&outconf, conf.sys_incpath, conf.delim, 'I');
1768+
}
1769+
1770+
if (!print_syslib) {
1771+
insertsyspath(&outconf, conf.sys_libpath, conf.delim, 'L');
1772+
}
1773+
17331774
for (Size i = 0; i < nargs; i++) {
17341775
process(a, &proc, args[i]);
17351776
}

0 commit comments

Comments
 (0)