Repository: conan-io/conan

Issue:
fPIC auto-managed in autotools build helper
The same we did with CMake here https://github.com/conan-io/conan/pull/2818/files
Take into account that there is already a flag fPIC. So, act when the parameter is not specified.



Discussion:

Resolving patch:
diff --git a/conans/client/build/autotools_environment.py b/conans/client/build/autotools_environment.py
--- a/conans/client/build/autotools_environment.py
+++ b/conans/client/build/autotools_environment.py
@@ -33,6 +33,7 @@ def __init__(self, conanfile, win_bash=False, include_rpath_flags=False):
         self._include_rpath_flags = include_rpath_flags
         self.subsystem = OSInfo().detect_windows_subsystem() if self._win_bash else None
         self._deps_cpp_info = conanfile.deps_cpp_info
+        self._os = conanfile.settings.get_safe("os")
         self._arch = conanfile.settings.get_safe("arch")
         self._build_type = conanfile.settings.get_safe("build_type")
         self._compiler = conanfile.settings.get_safe("compiler")
@@ -55,34 +56,37 @@ def __init__(self, conanfile, win_bash=False, include_rpath_flags=False):
         self.cppstd_flag = cppstd_flag(self._compiler, self._compiler_version, self._cppstd)
         # Not -L flags, ["-m64" "-m32"]
         self.link_flags = self._configure_link_flags()  # TEST!
-        # Not declared by default
-        self.fpic = None
+        # Precalculate -fPIC
+        self.fpic = self._configure_fpic()
 
         # Precalculate build, host, target triplets
         self.build, self.host, self.target = self._get_host_build_target_flags()
 
+    def _configure_fpic(self):
+        if str(self._os) not in ["Windows", "WindowsStore"]:
+            fpic = self._conanfile.options.get_safe("fPIC")
+            if fpic is not None:
+                shared = self._conanfile.options.get_safe("shared")
+                return True if (fpic or shared) else None
+
     def _get_host_build_target_flags(self):
         """Based on google search for build/host triplets, it could need a lot
         and complex verification"""
 
         arch_detected = detected_architecture() or platform.machine()
         os_detected = platform.system()
-        arch_settings = self._conanfile.settings.get_safe("arch")
-        os_settings = self._conanfile.settings.get_safe("os")
-        compiler = self._conanfile.settings.get_safe("compiler")
 
-        if (os_detected is None or arch_detected is None or arch_settings is None or
-                os_settings is None):
+        if os_detected is None or arch_detected is None or self._arch is None or self._os is None:
             return False, False, False
         if not cross_building(self._conanfile.settings, os_detected, arch_detected):
             return False, False, False
 
         try:
-            build = get_gnu_triplet(os_detected, arch_detected, compiler)
+            build = get_gnu_triplet(os_detected, arch_detected, self._compiler)
         except ConanException:
             build = None
         try:
-            host = get_gnu_triplet(os_settings, arch_settings, compiler)
+            host = get_gnu_triplet(self._os, self._arch, self._compiler)
         except ConanException:
             host = None
         return build, host, None
@@ -184,8 +188,7 @@ def _configure_link_flags(self):
             ret.append(sysf)
 
         if self._include_rpath_flags:
-            the_os = self._conanfile.settings.get_safe("os_build") or \
-                 self._conanfile.settings.get_safe("os")
+            the_os = self._conanfile.settings.get_safe("os_build") or self._os
             ret.extend(rpath_flags(the_os, self._compiler, self._deps_cpp_info.lib_paths))
 
         return ret


Test patch:
diff --git a/conans/test/build_helpers/autotools_configure_test.py b/conans/test/build_helpers/autotools_configure_test.py
--- a/conans/test/build_helpers/autotools_configure_test.py
+++ b/conans/test/build_helpers/autotools_configure_test.py
@@ -543,3 +543,40 @@ def custom_configure(obj, configure_dir=None, args=None, build=None, host=None,
             # TEST with custom vars
             mocked_result = be.configure(vars=my_vars)
             self.assertEqual(mocked_result, my_vars)
+
+    def autotools_fpic_test(self):
+        runner = None
+        settings = MockSettings({"os": "Linux"})
+        options = MockOptions({"fPIC": True, "shared": False})
+        conanfile = MockConanfile(settings, options, runner)
+        ab = AutoToolsBuildEnvironment(conanfile)
+        self.assertTrue(ab.fpic)
+
+        options = MockOptions({"fPIC": True, "shared": True})
+        conanfile = MockConanfile(settings, options, runner)
+        ab = AutoToolsBuildEnvironment(conanfile)
+        self.assertTrue(ab.fpic)
+
+        options = MockOptions({"fPIC": False, "shared": True})
+        conanfile = MockConanfile(settings, options, runner)
+        ab = AutoToolsBuildEnvironment(conanfile)
+        self.assertTrue(ab.fpic)
+
+        options = MockOptions({"fPIC": False, "shared": False})
+        conanfile = MockConanfile(settings, options, runner)
+        ab = AutoToolsBuildEnvironment(conanfile)
+        self.assertFalse(ab.fpic)
+
+        settings = MockSettings({"os": "Windows"})
+        options = MockOptions({"fPIC": True, "shared": True})
+        conanfile = MockConanfile(settings, options, runner)
+        ab = AutoToolsBuildEnvironment(conanfile)
+        self.assertFalse(ab.fpic)
+
+        settings = MockSettings({"os": "Macos", "compiler": "clang"})
+        options = MockOptions({"fPIC": False, "shared": False})
+        conanfile = MockConanfile(settings, options, runner)
+        ab = AutoToolsBuildEnvironment(conanfile)
+        self.assertFalse(ab.fpic)
+        ab.fpic = True
+        self.assertIn("-fPIC", ab.vars["CXXFLAGS"])