@@ -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.
15431572static 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