Repository: conan-io/conan

Issue:
[bug] -rpath flag separator depends on build OS instead of host OS
<!--
  Please don't forget to update the issue title.
  Include all applicable information to help us reproduce your problem.

  To help us debug your issue please explain:
-->

### Environment Details (include every applicable attribute)
  * Operating System+version: Linux Mint 20 Ulyana 
  * Compiler+version: clang version 10.0.0, Target: x86_64-apple-darwin19
  * Conan version: 1.29.0 with PR #7695 applied
  * Python version: 3.8.2

### Steps to reproduce (Include if Applicable)
Install [osxcross](https://github.com/tpoechtrager/osxcross) on Linux, create a profile with `os=Macos` and attempt to build `glib` with [this PR](https://github.com/conan-io/conan-center-index/pull/2925) applied, like this:

### Logs (Executed commands with output) (Include/Attach if Applicable)
```sh
conan create . -pr:h x86_64-apple-darwin19-macosx-clang -pr:b default -o glib:with_elf=False
```
results in
```
[111/398] Linking target glib/libglib-2.0.0.dylib
FAILED: glib/libglib-2.0.0.dylib 
x86_64-apple-darwin19-clang  -o glib/libglib-2.0.0.dylib ... lots of stuff ... -Wl,-rpath=/home/niklas.guertler/.conan/data/pcre/8.41/_/_/package/ddd42dd21f101343926e8ee9dff966bdba84b078/lib /home/niklas.guertler/.conan/data/bzip2/1.0.8/_/_/package/d81a2c40ef3ca9d7c9333cd75ac1bcdef9183a77/lib/libbz2.a -Wl,-rpath=/home/niklas.guertler/.conan/data/bzip2/1.0.8/_/_/package/d81a2c40ef3ca9d7c9333cd75ac1bcdef9183a77/lib /home/niklas.guertler/.conan/data/zlib/1.2.11/_/_/package/099680e166ebfb4e7a7b9e0126a86c3d8bf1e7a7/lib/libz.a -Wl,-rpath=/home/niklas.guertler/.conan/data/zlib/1.2.11/_/_/package/099680e166ebfb4e7a7b9e0126a86c3d8bf1e7a7/lib -lgnuintl -liconv -liconv
ld: unknown option: -rpath=/home/niklas.guertler/.conan/data/pcre/8.41/_/_/package/ddd42dd21f101343926e8ee9dff966bdba84b078/lib
clang-10: error: linker command failed with exit code 1 (use -v to see invocation)
```
The `libpcre.pc` file created by the `pkg_config` generator looks like this:
```
prefix=/home/niklas.guertler/.conan/data/pcre/8.41/_/_/package/ddd42dd21f101343926e8ee9dff966bdba84b078
libdir=${prefix}/lib
includedir=${prefix}/include

Name: pcre-libpcre
Description: Conan package: pcre-libpcre
Version: 8.41
Libs: -L${libdir} -lpcre  -Wl,-rpath="${libdir}"
Cflags: -I${includedir} -DPCRE_STATIC=1
Requires: bzip2 zlib
```
The `ld64` linker used by Clang to (cross-)compile for Mac OS doesn't understand rpath flags with a `=` separator, which is why the [`rpath_flags` function](https://github.com/conan-io/conan/blob/c829789c2cd96a888494e6b994de37b52d26e707/conans/client/build/compiler_flags.py#L31) uses `,` instead of `=` when compiling _on_ Mac OS. However, this does not work when cross-compiling on Linux _for_ Mac OS, resulting in the malformed `.pc` files.

I would suggest changing the `rpath_flags` function to take the host (instead of build) OS as parameter and use that to determine the `rpath_separator`. I will submit a PR that does that.


Discussion:

Resolving patch:
diff --git a/conans/client/build/compiler_flags.py b/conans/client/build/compiler_flags.py
--- a/conans/client/build/compiler_flags.py
+++ b/conans/client/build/compiler_flags.py
@@ -28,7 +28,7 @@ def rpath_flags(settings, os_build, lib_paths):
     if not os_build:
         return []
     if compiler in GCC_LIKE:
-        rpath_separator = "," if is_apple_os(os_build) else "="
+        rpath_separator = ","
         return ['-Wl,-rpath%s"%s"' % (rpath_separator, x.replace("\\", "/"))
                 for x in lib_paths if x]
     return []


Test patch:
diff --git a/conans/test/functional/generators/pkg_config_test.py b/conans/test/functional/generators/pkg_config_test.py
--- a/conans/test/functional/generators/pkg_config_test.py
+++ b/conans/test/functional/generators/pkg_config_test.py
@@ -44,9 +44,7 @@ def package_info(self):
         self.assertTrue(os.path.exists(pc_path))
         pc_content = load(pc_path)
         expected_rpaths = ""
-        if platform.system() == "Linux":
-            expected_rpaths = ' -Wl,-rpath="${libdir}" -Wl,-rpath="${libdir2}"'
-        elif platform.system() == "Darwin":
+        if platform.system() in ("Linux", "Darwin"):
             expected_rpaths = ' -Wl,-rpath,"${libdir}" -Wl,-rpath,"${libdir2}"'
         expected_content = """libdir=/my_absoulte_path/fake/mylib/lib
 libdir2=${prefix}/lib2
@@ -138,7 +136,7 @@ def package_info(self):
         pc_path = os.path.join(client.current_folder, "MyLib.pc")
         self.assertTrue(os.path.exists(pc_path))
         pc_content = load(pc_path)
-        self.assertIn("-Wl,-rpath=\"${libdir}\"", pc_content)
+        self.assertIn("-Wl,-rpath,\"${libdir}\"", pc_content)
 
     def test_system_libs(self):
         conanfile = """
diff --git a/conans/test/unittests/client/build/autotools_environment_test.py b/conans/test/unittests/client/build/autotools_environment_test.py
--- a/conans/test/unittests/client/build/autotools_environment_test.py
+++ b/conans/test/unittests/client/build/autotools_environment_test.py
@@ -388,7 +388,7 @@ def test_rpath_optin(self):
                     'CXXFLAGS': 'a_c_flag -m64 -O3 -s --sysroot=/path/to/folder a_cxx_flag',
                     'LDFLAGS': 'shared_link_flag exe_link_flag -framework oneframework -framework twoframework '
                                '-F one/framework/path -m64 --sysroot=/path/to/folder '
-                               '-Wl,-rpath="one/lib/path" -Lone/lib/path',
+                               '-Wl,-rpath,"one/lib/path" -Lone/lib/path',
                     'LIBS': '-lonelib -ltwolib -lonesystemlib -ltwosystemlib'}
         be = AutoToolsBuildEnvironment(conanfile, include_rpath_flags=True)
         self.assertEqual(be.vars, expected)
diff --git a/conans/test/unittests/client/generators/compiler_args_test.py b/conans/test/unittests/client/generators/compiler_args_test.py
--- a/conans/test/unittests/client/generators/compiler_args_test.py
+++ b/conans/test/unittests/client/generators/compiler_args_test.py
@@ -76,7 +76,7 @@ def test_gcc(self):
         gcc = GCCGenerator(conan_file)
         self.assertEqual('-Dmydefine1 -I/root/include -I/root/path/to/include1'
                          ' cxx_flag1 c_flag1 -m32 -O3 -s -DNDEBUG'
-                         ' -Wl,-rpath="/root/lib" -Wl,-rpath="/root/path/to/lib1"'
+                         ' -Wl,-rpath,"/root/lib" -Wl,-rpath,"/root/path/to/lib1"'
                          ' -L/root/lib -L/root/path/to/lib1 -lmylib'
                          ' -F /root/Frameworks -std=gnu++17', gcc.content)
 
@@ -87,7 +87,7 @@ def test_gcc(self):
         gcc = GCCGenerator(conan_file)
         self.assertEqual('-Dmydefine1 -I/root/include -I/root/path/to/include1'
                          ' cxx_flag1 c_flag1 -m64 -g'
-                         ' -Wl,-rpath="/root/lib" -Wl,-rpath="/root/path/to/lib1"'
+                         ' -Wl,-rpath,"/root/lib" -Wl,-rpath,"/root/path/to/lib1"'
                          ' -L/root/lib -L/root/path/to/lib1 -lmylib'
                          ' -D_GLIBCXX_USE_CXX11_ABI=1 -F /root/Frameworks -std=gnu++17',
                          gcc.content)
@@ -96,7 +96,7 @@ def test_gcc(self):
         gcc = GCCGenerator(conan_file)
         self.assertEqual('-Dmydefine1 -I/root/include -I/root/path/to/include1'
                          ' cxx_flag1 c_flag1 -m64 -g'
-                         ' -Wl,-rpath="/root/lib" -Wl,-rpath="/root/path/to/lib1"'
+                         ' -Wl,-rpath,"/root/lib" -Wl,-rpath,"/root/path/to/lib1"'
                          ' -L/root/lib -L/root/path/to/lib1 -lmylib'
                          ' -D_GLIBCXX_USE_CXX11_ABI=0 -F /root/Frameworks -std=gnu++17',
                          gcc.content)
@@ -110,7 +110,7 @@ def test_gcc(self):
         # GCC generator ignores the compiler setting, it is always gcc
         self.assertEqual('-Dmydefine1 -I/root/include -I/root/path/to/include1'
                          ' cxx_flag1 c_flag1 -m32 -O3 -s -DNDEBUG'
-                         ' -Wl,-rpath="/root/lib" -Wl,-rpath="/root/path/to/lib1"'
+                         ' -Wl,-rpath,"/root/lib" -Wl,-rpath,"/root/path/to/lib1"'
                          ' -L/root/lib -L/root/path/to/lib1 -lmylib'
                          ' -D_GLIBCXX_USE_CXX11_ABI=0 -F /root/Frameworks -std=gnu++17',
                          gcc.content)
@@ -190,6 +190,6 @@ def test_system_libs(self):
         args = CompilerArgsGenerator(conan_file)
         self.assertEqual('-Dmydefine1 -I/root/include -I/root/path/to/include1'
                          ' cxx_flag1 c_flag1 -m64 -O3 -s -DNDEBUG'
-                         ' -Wl,-rpath="/root/lib" -Wl,-rpath="/root/path/to/lib1"'
+                         ' -Wl,-rpath,"/root/lib" -Wl,-rpath,"/root/path/to/lib1"'
                          ' -L/root/lib -L/root/path/to/lib1 -lmylib -lsystem_lib1'
                          ' -F /root/Frameworks', args.content)