c1ec749
From 5126ff48d9ac54828d1947d1423a5ef2a8efee3b Mon Sep 17 00:00:00 2001
c1ec749
From: David Seifert <soap@gentoo.org>
c1ec749
Date: Sat, 5 Oct 2019 15:58:45 +0200
c1ec749
Subject: [PATCH 1/2] Revert "Use pcfiledir for prefix in pkgconfig file"
c1ec749
c1ec749
The change makes implicit assumptions on the layout of the install
c1ec749
tree, which is going to break in many ways.
c1ec749
c1ec749
The correct solution is to use the `PKG_CONFIG_SYSROOT_DIR` variable
c1ec749
to inject the cross-compiled sysroot into `-I` and `-L` paths.
c1ec749
---
c1ec749
 googlemock/cmake/gmock.pc.in      | 5 ++---
c1ec749
 googlemock/cmake/gmock_main.pc.in | 5 ++---
c1ec749
 googletest/cmake/gtest.pc.in      | 5 ++---
c1ec749
 googletest/cmake/gtest_main.pc.in | 5 ++---
c1ec749
 4 files changed, 8 insertions(+), 12 deletions(-)
c1ec749
c1ec749
diff --git a/googlemock/cmake/gmock.pc.in b/googlemock/cmake/gmock.pc.in
c1ec749
index 08e045474..2ef0fbca1 100644
c1ec749
--- a/googlemock/cmake/gmock.pc.in
c1ec749
+++ b/googlemock/cmake/gmock.pc.in
c1ec749
@@ -1,6 +1,5 @@
c1ec749
-prefix=${pcfiledir}/../..
c1ec749
-libdir=${prefix}/@CMAKE_INSTALL_LIBDIR@
c1ec749
-includedir=${prefix}/@CMAKE_INSTALL_INCLUDEDIR@
c1ec749
+libdir=@CMAKE_INSTALL_FULL_LIBDIR@
c1ec749
+includedir=@CMAKE_INSTALL_FULL_INCLUDEDIR@
c1ec749
 
c1ec749
 Name: gmock
c1ec749
 Description: GoogleMock (without main() function)
c1ec749
diff --git a/googlemock/cmake/gmock_main.pc.in b/googlemock/cmake/gmock_main.pc.in
c1ec749
index b22fe6148..04658fe2e 100644
c1ec749
--- a/googlemock/cmake/gmock_main.pc.in
c1ec749
+++ b/googlemock/cmake/gmock_main.pc.in
c1ec749
@@ -1,6 +1,5 @@
c1ec749
-prefix=${pcfiledir}/../..
c1ec749
-libdir=${prefix}/@CMAKE_INSTALL_LIBDIR@
c1ec749
-includedir=${prefix}/@CMAKE_INSTALL_INCLUDEDIR@
c1ec749
+libdir=@CMAKE_INSTALL_FULL_LIBDIR@
c1ec749
+includedir=@CMAKE_INSTALL_FULL_INCLUDEDIR@
c1ec749
 
c1ec749
 Name: gmock_main
c1ec749
 Description: GoogleMock (with main() function)
c1ec749
diff --git a/googletest/cmake/gtest.pc.in b/googletest/cmake/gtest.pc.in
c1ec749
index 9aae29e26..e7967ad56 100644
c1ec749
--- a/googletest/cmake/gtest.pc.in
c1ec749
+++ b/googletest/cmake/gtest.pc.in
c1ec749
@@ -1,6 +1,5 @@
c1ec749
-prefix=${pcfiledir}/../..
c1ec749
-libdir=${prefix}/@CMAKE_INSTALL_LIBDIR@
c1ec749
-includedir=${prefix}/@CMAKE_INSTALL_INCLUDEDIR@
c1ec749
+libdir=@CMAKE_INSTALL_FULL_LIBDIR@
c1ec749
+includedir=@CMAKE_INSTALL_FULL_INCLUDEDIR@
c1ec749
 
c1ec749
 Name: gtest
c1ec749
 Description: GoogleTest (without main() function)
c1ec749
diff --git a/googletest/cmake/gtest_main.pc.in b/googletest/cmake/gtest_main.pc.in
c1ec749
index 915f2973a..fe25d9c73 100644
c1ec749
--- a/googletest/cmake/gtest_main.pc.in
c1ec749
+++ b/googletest/cmake/gtest_main.pc.in
c1ec749
@@ -1,6 +1,5 @@
c1ec749
-prefix=${pcfiledir}/../..
c1ec749
-libdir=${prefix}/@CMAKE_INSTALL_LIBDIR@
c1ec749
-includedir=${prefix}/@CMAKE_INSTALL_INCLUDEDIR@
c1ec749
+libdir=@CMAKE_INSTALL_FULL_LIBDIR@
c1ec749
+includedir=@CMAKE_INSTALL_FULL_INCLUDEDIR@
c1ec749
 
c1ec749
 Name: gtest_main
c1ec749
 Description: GoogleTest (with main() function)
c1ec749
c1ec749
From b96832a44b4994cafc824f200d69232d79f30d81 Mon Sep 17 00:00:00 2001
c1ec749
From: David Seifert <soap@gentoo.org>
c1ec749
Date: Sat, 5 Oct 2019 15:58:48 +0200
c1ec749
Subject: [PATCH 2/2] Add documentation for pkg-config in cross-compilation
c1ec749
 settings
c1ec749
c1ec749
---
c1ec749
 googletest/docs/pkgconfig.md | 78 ++++++++++++++++++++++++++++++++++++
c1ec749
 1 file changed, 78 insertions(+)
c1ec749
c1ec749
diff --git a/googletest/docs/pkgconfig.md b/googletest/docs/pkgconfig.md
c1ec749
index 6dc067388..d5f361448 100644
c1ec749
--- a/googletest/docs/pkgconfig.md
c1ec749
+++ b/googletest/docs/pkgconfig.md
c1ec749
@@ -139,3 +139,81 @@ export PKG_CONFIG_PATH=/usr/local/lib64/pkgconfig
c1ec749
 ```
c1ec749
 
c1ec749
 pkg-config will also try to look in `PKG_CONFIG_PATH` to find `gtest_main.pc`.
c1ec749
+
c1ec749
+### Using pkg-config in a cross-compilation setting
c1ec749
+
c1ec749
+Pkg-config can be used in a cross-compilation setting too. To do this, let's
c1ec749
+assume the final prefix of the cross-compiled installation will be `/usr`,
c1ec749
+and your sysroot is `/home/MYUSER/sysroot`. Configure and install GTest using
c1ec749
+
c1ec749
+```
c1ec749
+mkdir build && cmake -DCMAKE_INSTALL_PREFIX=/usr ..
c1ec749
+```
c1ec749
+
c1ec749
+Install into the sysroot using `DESTDIR`:
c1ec749
+
c1ec749
+```
c1ec749
+make -j install DESTDIR=/home/MYUSER/sysroot
c1ec749
+```
c1ec749
+
c1ec749
+Before we continue, it is recommended to **always** define the following two
c1ec749
+variables for pkg-config in a cross-compilation setting:
c1ec749
+
c1ec749
+```
c1ec749
+export PKG_CONFIG_ALLOW_SYSTEM_CFLAGS=yes
c1ec749
+export PKG_CONFIG_ALLOW_SYSTEM_LIBS=yes
c1ec749
+```
c1ec749
+
c1ec749
+otherwise `pkg-config` will filter `-I` and `-L` flags against standard
c1ec749
+prefixes such as `/usr` (see https://bugs.freedesktop.org/show_bug.cgi?id=28264#c3
c1ec749
+for reasons why this stripping needs to occur usually).
c1ec749
+
c1ec749
+If you look at the generated pkg-config file, it will look something like
c1ec749
+
c1ec749
+```
c1ec749
+libdir=/usr/lib64
c1ec749
+includedir=/usr/include
c1ec749
+
c1ec749
+Name: gtest
c1ec749
+Description: GoogleTest (without main() function)
c1ec749
+Version: 1.10.0
c1ec749
+URL: https://github.com/google/googletest
c1ec749
+Libs: -L${libdir} -lgtest -lpthread
c1ec749
+Cflags: -I${includedir} -DGTEST_HAS_PTHREAD=1 -lpthread
c1ec749
+```
c1ec749
+
c1ec749
+Notice that the sysroot is not included in `libdir` and `includedir`! If you
c1ec749
+try to run `pkg-config` with the correct
c1ec749
+`PKG_CONFIG_LIBDIR=/home/MYUSER/sysroot/usr/lib64/pkgconfig` against this `.pc`
c1ec749
+file, you will get
c1ec749
+
c1ec749
+```
c1ec749
+$ pkg-config --cflags gtest
c1ec749
+-DGTEST_HAS_PTHREAD=1 -lpthread -I/usr/include
c1ec749
+$ pkg-config --libs gtest
c1ec749
+-L/usr/lib64 -lgtest -lpthread
c1ec749
+```
c1ec749
+
c1ec749
+which is obviously wrong and points to the `CBUILD` and not `CHOST` root.
c1ec749
+In order to use this in a cross-compilation setting, we need to tell
c1ec749
+pkg-config to inject the actual sysroot into `-I` and `-L` variables. Let us
c1ec749
+now tell pkg-config about the actual sysroot
c1ec749
+
c1ec749
+```
c1ec749
+export PKG_CONFIG_DIR=
c1ec749
+export PKG_CONFIG_SYSROOT_DIR=/home/MYUSER/sysroot
c1ec749
+export PKG_CONFIG_LIBDIR=${PKG_CONFIG_SYSROOT_DIR}/usr/lib64/pkgconfig
c1ec749
+```
c1ec749
+
c1ec749
+and running `pkg-config` again we get
c1ec749
+
c1ec749
+```
c1ec749
+$ pkg-config --cflags gtest
c1ec749
+-DGTEST_HAS_PTHREAD=1 -lpthread -I/home/MYUSER/sysroot/usr/include
c1ec749
+$ pkg-config --libs gtest
c1ec749
+-L/home/MYUSER/sysroot/usr/lib64 -lgtest -lpthread
c1ec749
+```
c1ec749
+
c1ec749
+which contains the correct sysroot now. For a more comprehensive guide to
c1ec749
+also including `${CHOST}` in build system calls, see the excellent tutorial
c1ec749
+by Diego Elio Pettenò: https://autotools.io/pkgconfig/cross-compiling.html