def test_arch_flag(self): for compiler in ("gcc", "clang", "sun-cc"): arch_flag = architecture_flag(arch='x86', compiler=compiler) self.assertEqual(arch_flag, '-m32') arch_flag = architecture_flag(arch='sparc', compiler='sun-cc') self.assertEqual(arch_flag, '-m32') for compiler in ("gcc", "clang", "sun-cc"): arch_flag = architecture_flag(arch='x86_64', compiler=compiler) self.assertEqual(arch_flag, '-m64') arch_flag = architecture_flag(arch='sparcv9', compiler='sun-cc') self.assertEqual(arch_flag, '-m64') for compiler in ("gcc", "clang", "sun-cc"): arch_flag = architecture_flag(arch='armv7', compiler=compiler) self.assertEqual(arch_flag, '') for compiler in ("gcc", "clang", "sun-cc"): arch_flag = architecture_flag(arch='s390', compiler=compiler) self.assertEqual(arch_flag, '-m31') for compiler in ("gcc", "clang", "sun-cc"): arch_flag = architecture_flag(arch='s390x', compiler=compiler) self.assertEqual(arch_flag, '-m64') arch_flag = architecture_flag(arch='x86', compiler='Visual Studio') self.assertEqual(arch_flag, '') arch_flag = architecture_flag(arch='x86_64', compiler='Visual Studio') self.assertEqual(arch_flag, '')
def test_arch_flag(self, compiler, arch, the_os, flag): settings = MockSettings({ "compiler": compiler, "arch": arch, "os": the_os }) self.assertEqual(architecture_flag(settings), flag)
def test_arch_flag_intel(self, base, arch, flag): settings = MockSettings({ "compiler": "intel", "compiler.base": base, "arch": arch }) self.assertEqual(architecture_flag(settings), flag)
def test_arch_flag_mcst_lcc(self, arch, flag): settings = MockSettings({ "compiler": "mcst-lcc", "compiler.base": "gcc", "arch": arch }) self.assertEqual(architecture_flag(settings), flag)
def __init__(self, conanfile): self._conanfile = conanfile self._build_type = conanfile.settings.get_safe("build_type") self._compiler = conanfile.settings.get_safe("compiler") self._compiler_version = conanfile.settings.get_safe( "compiler.version") self._compiler_runtime = conanfile.settings.get_safe( "compiler.runtime") self._shared = self._conanfile.options.get_safe("shared") self._fpic = self._deduce_fpic() self._libcxx_flag = libcxx_flag(conanfile.settings) self._cppstd_flag = cppstd_flag(conanfile.settings) self._skip_rpath = True if self._conanfile.settings.get_safe( "os") == "Macos" else False self._arch_flag = architecture_flag(self._conanfile.settings) self._build_type_flags = build_type_flags(self._conanfile.settings) self._os_host = conanfile.settings.get_safe("os") self._arch_host = conanfile.settings.get_safe("arch") self._os_target, self._arch_target = get_target_os_arch(conanfile) self._arch_build, self._os_build = self._get_build_os_arch() self._trip_build, self._trip_host, self._trip_target = self._get_host_build_target_flags( ) self._build_type_define = build_type_define( build_type=self._build_type) self._glibcxx_define = libcxx_define(self._conanfile.settings) self.variables = {} self.preprocessor_definitions = {}
def _configure_link_flags(self): """Not the -L""" ret = copy.copy(self._deps_cpp_info.sharedlinkflags) ret.extend(self._deps_cpp_info.exelinkflags) ret.extend( format_frameworks(self._deps_cpp_info.frameworks, self._conanfile.settings)) ret.extend( format_framework_paths(self._deps_cpp_info.framework_paths, self._conanfile.settings)) arch_flag = architecture_flag(self._conanfile.settings) if arch_flag: ret.append(arch_flag) sysf = sysroot_flag(self._deps_cpp_info.sysroot, self._conanfile.settings, win_bash=self._win_bash, subsystem=self.subsystem) if sysf: ret.append(sysf) if self._include_rpath_flags: os_build, _ = get_build_os_arch(self._conanfile) if not hasattr(self._conanfile, 'settings_build'): os_build = os_build or self._os ret.extend( rpath_flags(self._conanfile.settings, os_build, self._deps_cpp_info.lib_paths)) return ret
def _configure_link_flags(self): """Not the -L""" ret = copy.copy(self._deps_cpp_info.sharedlinkflags) ret.extend(self._deps_cpp_info.exelinkflags) ret.extend( format_frameworks(self._deps_cpp_info.frameworks, compiler=self._compiler)) ret.extend( format_framework_paths(self._deps_cpp_info.framework_paths, compiler=self._compiler)) arch_flag = architecture_flag(compiler=self._compiler, os=self._os, arch=self._arch) if arch_flag: ret.append(arch_flag) sysf = sysroot_flag(self._deps_cpp_info.sysroot, win_bash=self._win_bash, subsystem=self.subsystem, compiler=self._compiler) if sysf: ret.append(sysf) if self._include_rpath_flags: 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
def _get_cmake_definitions(self): def add_cmake_flag(cmake_flags, name, flag): """ appends compiler linker flags (if already present), or just sets """ if flag: if name not in cmake_flags: cmake_flags[name] = flag else: cmake_flags[name] = ' ' + flag return cmake_flags ret = OrderedDict() ret.update(self._build_type_definition()) ret.update(self._runtime_definition()) ret.update(self._cmake_compiler_options()) ret.update(self._cmake_cross_build_defines()) ret.update(self._get_cpp_standard_vars()) ret["CONAN_EXPORTED"] = "1" if self._compiler: ret["CONAN_COMPILER"] = self._compiler if self._compiler_version: ret["CONAN_COMPILER_VERSION"] = str(self._compiler_version) # Force compiler flags -- TODO: give as environment/setting parameter? arch_flag = architecture_flag(compiler=self._compiler, arch=self._arch) ret = add_cmake_flag(ret, 'CONAN_CXX_FLAGS', arch_flag) ret = add_cmake_flag(ret, 'CONAN_SHARED_LINKER_FLAGS', arch_flag) ret = add_cmake_flag(ret, 'CONAN_C_FLAGS', arch_flag) if self._set_cmake_flags: ret = add_cmake_flag(ret, 'CMAKE_CXX_FLAGS', arch_flag) ret = add_cmake_flag(ret, 'CMAKE_SHARED_LINKER_FLAGS', arch_flag) ret = add_cmake_flag(ret, 'CMAKE_C_FLAGS', arch_flag) if self._libcxx: ret["CONAN_LIBCXX"] = self._libcxx # Shared library try: ret["BUILD_SHARED_LIBS"] = "ON" if self._conanfile.options.shared else "OFF" except ConanException: pass # Install to package folder try: if self._conanfile.package_folder: ret["CMAKE_INSTALL_PREFIX"] = self._conanfile.package_folder except AttributeError: pass if str(self._os) in ["Windows", "WindowsStore" ] and self._compiler == "Visual Studio": if self.parallel: cpus = tools.cpu_count() ret["CONAN_CXX_FLAGS"] = "/MP%s" % cpus ret["CONAN_C_FLAGS"] = "/MP%s" % cpus return ret
def test_catalyst(self): settings = MockSettings({ "compiler": "apple-clang", "arch": "x86_64", "os": "Macos", "os.subsystem": "catalyst" }) self.assertEqual(architecture_flag(settings), "--target=x86_64-apple-ios-macabi")
def content(self): """With compiler_args you can invoke your compiler: $ gcc main.c @conanbuildinfo.args -o main $ clang main.c @conanbuildinfo.args -o main $ cl /EHsc main.c @conanbuildinfo.args """ flags = [] flags.extend(format_defines(self._deps_build_info.defines)) flags.extend(format_include_paths(self._deps_build_info.include_paths, compiler=self.compiler)) flags.extend(self._deps_build_info.cxxflags) flags.extend(self._deps_build_info.cflags) arch_flag = architecture_flag(arch=self.conanfile.settings.get_safe("arch"), os=self.conanfile.settings.get_safe("os"), compiler=self.compiler) if arch_flag: flags.append(arch_flag) build_type = self.conanfile.settings.get_safe("build_type") btfs = build_type_flags(compiler=self.compiler, build_type=build_type, vs_toolset=self.conanfile.settings.get_safe("compiler.toolset")) if btfs: flags.extend(btfs) btd = build_type_define(build_type=build_type) if btd: flags.extend(format_defines([btd])) if self.compiler == "Visual Studio": runtime = visual_runtime(self.conanfile.settings.get_safe("compiler.runtime")) if runtime: flags.append(runtime) # Necessary in the "cl" invocation before specify the rest of linker flags flags.append(visual_linker_option_separator) os_build, _ = get_build_os_arch(self.conanfile) if not hasattr(self.conanfile, 'settings_build'): os_build = os_build or self.conanfile.settings.get_safe("os") flags.extend(rpath_flags(os_build, self.compiler, self._deps_build_info.lib_paths)) flags.extend(format_library_paths(self._deps_build_info.lib_paths, compiler=self.compiler)) flags.extend(format_libraries(self._deps_build_info.libs, compiler=self.compiler)) flags.extend(format_libraries(self._deps_build_info.system_libs, compiler=self.compiler)) flags.extend(self._deps_build_info.sharedlinkflags) flags.extend(self._deps_build_info.exelinkflags) flags.extend(self._libcxx_flags()) flags.extend(format_frameworks(self._deps_build_info.frameworks, compiler=self.compiler)) flags.extend(format_framework_paths(self._deps_build_info.framework_paths, compiler=self.compiler)) flags.append(cppstd_flag(self.conanfile.settings)) sysrf = sysroot_flag(self._deps_build_info.sysroot, compiler=self.compiler) if sysrf: flags.append(sysrf) return " ".join(flag for flag in flags if flag)
def content(self): """With compiler_args you can invoke your compiler: $ gcc main.c @conanbuildinfo.args -o main $ clang main.c @conanbuildinfo.args -o main $ cl /EHsc main.c @conanbuildinfo.args """ flags = [] flags.extend(format_defines(self._deps_build_info.defines)) flags.extend(format_include_paths(self._deps_build_info.include_paths, compiler=self.compiler)) flags.extend(self._deps_build_info.cppflags) flags.extend(self._deps_build_info.cflags) arch_flag = architecture_flag(arch=self.conanfile.settings.get_safe("arch"), compiler=self.compiler) if arch_flag: flags.append(arch_flag) build_type = self.conanfile.settings.get_safe("build_type") btfs = build_type_flags(compiler=self.compiler, build_type=build_type, vs_toolset=self.conanfile.settings.get_safe("compiler.toolset")) if btfs: flags.extend(btfs) btd = build_type_define(build_type=build_type) if btd: flags.extend(format_defines([btd])) if self.compiler == "Visual Studio": runtime = visual_runtime(self.conanfile.settings.get_safe("compiler.runtime")) if runtime: flags.append(runtime) # Necessary in the "cl" invocation before specify the rest of linker flags flags.append(visual_linker_option_separator) the_os = (self.conanfile.settings.get_safe("os_build") or self.conanfile.settings.get_safe("os")) flags.extend(rpath_flags(the_os, self.compiler, self._deps_build_info.lib_paths)) flags.extend(format_library_paths(self._deps_build_info.lib_paths, compiler=self.compiler)) flags.extend(format_libraries(self._deps_build_info.libs, compiler=self.compiler)) flags.extend(self._deps_build_info.sharedlinkflags) flags.extend(self._deps_build_info.exelinkflags) flags.extend(self._libcxx_flags()) flags.append(cppstd_flag(self.conanfile.settings.get_safe("compiler"), self.conanfile.settings.get_safe("compiler.version"), self.conanfile.settings.get_safe("cppstd"))) sysrf = sysroot_flag(self._deps_build_info.sysroot, compiler=self.compiler) if sysrf: flags.append(sysrf) return " ".join(flag for flag in flags if flag)
def _configure_flags(self): ret = copy.copy(self._deps_cpp_info.cflags) arch_flag = architecture_flag(compiler=self._compiler, arch=self._arch) if arch_flag: ret.append(arch_flag) btfs = build_type_flags(compiler=self._compiler, build_type=self._build_type, vs_toolset=self._conanfile.settings.get_safe("compiler.toolset")) if btfs: ret.extend(btfs) srf = sysroot_flag(self._deps_cpp_info.sysroot, win_bash=self._win_bash, subsystem=self.subsystem, compiler=self._compiler) if srf: ret.append(srf) return ret
def _configure_flags(self): ret = copy.copy(self._deps_cpp_info.cflags) arch_flag = architecture_flag(compiler=self._compiler, arch=self._arch) if arch_flag: ret.append(arch_flag) btf = build_type_flag(compiler=self._compiler, build_type=self._build_type) if btf: ret.append(btf) srf = sysroot_flag(self._deps_cpp_info.sysroot, win_bash=self._win_bash, subsystem=self.subsystem, compiler=self._compiler) if srf: ret.append(srf) return ret
def _configure_flags(self): ret = copy.copy(self._deps_cpp_info.cflags) arch_flag = architecture_flag(self._conanfile.settings) if arch_flag: ret.append(arch_flag) btfs = build_type_flags(self._conanfile.settings) if btfs: ret.extend(btfs) srf = sysroot_flag(self._deps_cpp_info.sysroot, self._conanfile.settings, win_bash=self._win_bash, subsystem=self.subsystem) if srf: ret.append(srf) if self._compiler_runtime: ret.append("-%s" % self._compiler_runtime) return ret
def _configure_link_flags(self): """Not the -L""" ret = copy.copy(self._deps_cpp_info.sharedlinkflags) ret.extend(self._deps_cpp_info.exelinkflags) arch_flag = architecture_flag(compiler=self._compiler, arch=self._arch) if arch_flag: ret.append(arch_flag) sysf = sysroot_flag(self._deps_cpp_info.sysroot, win_bash=self._win_bash, subsystem=self.subsystem, compiler=self._compiler) if sysf: ret.append(sysf) if self._include_rpath_flags: the_os = self._conanfile.settings.get_safe("os_build") or \ self._conanfile.settings.get_safe("os") ret.extend(rpath_flags(the_os, self._compiler, self._deps_cpp_info.lib_paths)) return ret
def __init__(self, conanfile): self._conanfile = conanfile self._set_libcxx = True self._set_cppstd = True self._set_arch = True self._set_shared = True if conanfile.options.get_safe( "shared") else False self._set_fpic = True if conanfile.options.get_safe("fPIC") else False self._compiler = conanfile.settings.get_safe("compiler") self._compiler_version = conanfile.settings.get_safe( "compiler.version") self._compiler_runtime = conanfile.settings.get_safe( "compiler.runtime") self._libcxx = conanfile.settings.get_safe("compiler.libcxx") # cpp standard self._cppstd = cppstd_from_settings(conanfile.settings) self._cppstd_flag = cppstd_flag(conanfile.settings) # arch_build in compiler flag format self._arch_flag = architecture_flag(self._conanfile.settings) # build_type information in compiler flag format self._build_type_flags = " ".join( build_type_flags(self._conanfile.settings)) self._os_host = conanfile.settings.get_safe("os") self._arch_host = conanfile.settings.get_safe("arch") self._os_target, self._arch_target = get_target_os_arch(conanfile) self._arch_build, self._os_build = self._get_build_os_arch() self._build_type = conanfile.settings.get_safe("build_type") # Precalculate build, host, target triplets self._trip_build, self._trip_host, self._trip_target = self._get_host_build_target_flags( ) self.definitions = {}
def test_arch_flag(self): for compiler in ("gcc", "clang", "sun-cc"): arch_flag = architecture_flag(arch='x86', compiler=compiler) self.assertEquals(arch_flag, '-m32') arch_flag = architecture_flag(arch='sparc', compiler='sun-cc') self.assertEquals(arch_flag, '-m32') for compiler in ("gcc", "clang", "sun-cc"): arch_flag = architecture_flag(arch='x86_64', compiler=compiler) self.assertEquals(arch_flag, '-m64') arch_flag = architecture_flag(arch='sparcv9', compiler='sun-cc') self.assertEquals(arch_flag, '-m64') for compiler in ("gcc", "clang", "sun-cc"): arch_flag = architecture_flag(arch='armv7', compiler=compiler) self.assertEquals(arch_flag, '') arch_flag = architecture_flag(arch='x86', compiler='Visual Studio') self.assertEquals(arch_flag, '') arch_flag = architecture_flag(arch='x86_64', compiler='Visual Studio') self.assertEquals(arch_flag, '')
def get_definitions(self): compiler = self._ss("compiler") compiler_version = self._ss("compiler.version") arch = self._ss("arch") os_ = self._ss("os") libcxx = self._ss("compiler.libcxx") runtime = self._ss("compiler.runtime") build_type = self._ss("build_type") ret = OrderedDict() ret.update(build_type_definition(build_type, self.generator)) ret.update(runtime_definition(runtime)) if str(os_) == "Macos": if arch == "x86": ret["CMAKE_OSX_ARCHITECTURES"] = "i386" ret.update(self._cmake_cross_build_defines()) ret.update(self._get_cpp_standard_vars()) ret["CONAN_EXPORTED"] = "1" ret[cmake_in_local_cache_var_name] =\ in_local_cache_definition(self._conanfile.in_local_cache)[cmake_in_local_cache_var_name] if compiler: ret["CONAN_COMPILER"] = compiler if compiler_version: ret["CONAN_COMPILER_VERSION"] = str(compiler_version) # Force compiler flags -- TODO: give as environment/setting parameter? arch_flag = architecture_flag(compiler=compiler, arch=arch) ret = add_cmake_flag(ret, 'CONAN_CXX_FLAGS', arch_flag) ret = add_cmake_flag(ret, 'CONAN_SHARED_LINKER_FLAGS', arch_flag) ret = add_cmake_flag(ret, 'CONAN_C_FLAGS', arch_flag) if libcxx: ret["CONAN_LIBCXX"] = libcxx # Shared library try: ret["BUILD_SHARED_LIBS"] = "ON" if self._conanfile.options.shared else "OFF" except ConanException: pass # Install to package folder try: if self._conanfile.package_folder: ret["CMAKE_INSTALL_PREFIX"] = self._conanfile.package_folder ret["CMAKE_INSTALL_BINDIR"] = DEFAULT_BIN ret["CMAKE_INSTALL_SBINDIR"] = DEFAULT_BIN ret["CMAKE_INSTALL_LIBEXECDIR"] = DEFAULT_BIN ret["CMAKE_INSTALL_LIBDIR"] = DEFAULT_LIB ret["CMAKE_INSTALL_INCLUDEDIR"] = DEFAULT_INCLUDE ret["CMAKE_INSTALL_OLDINCLUDEDIR"] = DEFAULT_INCLUDE ret["CMAKE_INSTALL_DATAROOTDIR"] = DEFAULT_SHARE except AttributeError: pass if str(os_) in ["Windows", "WindowsStore" ] and compiler == "Visual Studio": if self._parallel: flag = parallel_compiler_cl_flag() ret = add_cmake_flag(ret, 'CONAN_CXX_FLAGS', flag) ret = add_cmake_flag(ret, 'CONAN_C_FLAGS', flag) # fpic if str(os_) not in ["Windows", "WindowsStore"]: fpic = self._conanfile.options.get_safe("fPIC") if fpic is not None: shared = self._conanfile.options.get_safe("shared") ret["CONAN_CMAKE_POSITION_INDEPENDENT_CODE"] = "ON" if ( fpic or shared) else "OFF" # Adjust automatically the module path in case the conanfile is using the cmake_find_package if "cmake_find_package" in self._conanfile.generators: ret["CMAKE_MODULE_PATH"] = self._conanfile.install_folder.replace( "\\", "/") ret.update(self._get_make_program_definition()) if self._set_cmake_flags: ret = add_cmake_flag(ret, 'CMAKE_CXX_FLAGS', arch_flag) ret = add_cmake_flag(ret, 'CMAKE_SHARED_LINKER_FLAGS', arch_flag) ret = add_cmake_flag(ret, 'CMAKE_C_FLAGS', arch_flag) # Disable CMake export registry #3070 (CMake installing modules in user home's) ret["CMAKE_EXPORT_NO_PACKAGE_REGISTRY"] = "ON" return ret
def test_toolchain_linux(self, build_type, arch, cppstd, libcxx, shared, fpic): settings_mock = _MockSettings(build_type, arch, cppstd, libcxx) conanfile = ConanFile(TestBufferConanOutput(), None) conanfile.options = {"shared": [True, False], "fPIC": [True, False]} conanfile.default_options = {"shared": shared, "fPIC": fpic} conanfile.initialize(settings_mock, EnvValues()) toolchain = MakeToolchain(conanfile) content = toolchain.content expected_template = Template( textwrap.dedent(""" # Conan generated toolchain file ifndef CONAN_TOOLCHAIN_INCLUDED CONAN_TOOLCHAIN_INCLUDED = TRUE CONAN_TC_BUILD_TYPE = {{build_type}} CONAN_TC_OS_HOST = None CONAN_TC_ARCH_HOST = {{arch_host}} CONAN_TC_TRIPLET_HOST = False CONAN_TC_OS_BUILD = Linux CONAN_TC_ARCH_BUILD = {{arch_build}} CONAN_TC_TRIPLET_BUILD = False CONAN_TC_OS_TARGET = None CONAN_TC_ARCH_TARGET = None CONAN_TC_TRIPLET_TARGET = None CONAN_TC_COMPILER = {{compiler}} CONAN_TC_COMPILER_VERSION = {{compiler_version}} CONAN_TC_COMPILER_RUNTIME = None CONAN_TC_LIBCXX = {{libcxx}} CONAN_TC_CPPSTD_FLAG = {{cppstd_flag}} CONAN_TC_ARCH_FLAG = {{arch_flag}} CONAN_TC_BUILD_TYPE_FLAGS = {{build_type_flags}} CONAN_TC_DEFINES ={{preserved_space}} CONAN_TC_SET_LIBCXX = True CONAN_TC_SET_CPPSTD = True CONAN_TC_SET_ARCH = True CONAN_TC_SET_FPIC = {{set_fpic}} CONAN_TC_SET_SHARED = {{set_shared}} CONAN_TC_CFLAGS += $(CONAN_TC_BUILD_TYPE_FLAGS) CONAN_TC_CXXFLAGS += $(CONAN_TC_BUILD_TYPE_FLAGS) ifeq ($(CONAN_TC_BUILD_TYPE),Release) CONAN_TC_DEFINES += NDEBUG endif ifeq ($(CONAN_TC_SET_LIBCXX),True) CONAN_TC_CLANG_BASED := $(if $(filter $(CONAN_TC_COMPILER),clang apple-clang),true) ifeq ($(CONAN_TC_CLANG_BASED),True) CONAN_TC_LIBSTDCXX_BASED := $(if $(filter $(CONAN_TC_LIBCXX),libstdc++ libstdc++11),true) ifeq ($(CONAN_TC_LIBSTDCXX_BASED),True) CONAN_TC_CXXFLAGS += -stdlib=libstdc++ else ifeq ($(CONAN_TC_LIBCXX),libc++) CONAN_TC_CXXFLAGS += -stdlib=libc++ endif else ifeq ($(CONAN_TC_COMPILER),sun-cc) ifeq ($(CONAN_TC_LIBCXX),libCstd) CONAN_TC_CXXFLAGS += -library=Cstd++ else ifeq ($(CONAN_TC_LIBCXX),libstdcxx) CONAN_TC_CXXFLAGS += -library=stdcxx4 else ifeq ($(CONAN_TC_LIBCXX),libstlport) CONAN_TC_CXXFLAGS += -library=stlport4 else ifeq ($(CONAN_TC_LIBCXX),libstdc++) CONAN_TC_CXXFLAGS += -library=stdcpp endif endif ifeq ($(CONAN_TC_LIBCXX),libstdc++11) CONAN_TC_DEFINES += GLIBCXX_USE_CXX11_ABI=1 else ifeq ($(CONAN_TC_LIBCXX),libstdc++) CONAN_TC_DEFINES += GLIBCXX_USE_CXX11_ABI=0 endif endif ifeq ($(CONAN_TC_SET_CPPSTD),True) CONAN_TC_CXXFLAGS += $(CONAN_TC_CPPSTD_FLAG) endif ifeq ($(CONAN_TC_SET_ARCH),True) CONAN_TC_CFLAGS += $(CONAN_TC_ARCH_FLAG) CONAN_TC_CXXFLAGS += $(CONAN_TC_ARCH_FLAG) CONAN_TC_SHARED_LINKER_FLAGS += $(CONAN_TC_ARCH_FLAG) CONAN_TC_EXE_LINKER_FLAGS += $(CONAN_TC_ARCH_FLAG) endif ifeq ($(CONAN_TC_SET_FPIC),True) CONAN_TC_CFLAGS += -fPIC CONAN_TC_CXXFLAGS += -fPIC CONAN_TC_SHARED_LINKER_FLAGS += -fPIC CONAN_TC_EXE_LINKER_FLAGS += -pie endif ifeq ($(CONAN_TC_SET_SHARED),True) CONAN_TC_LDFLAGS += -shared CONAN_TC_LDFLAGS += $(CONAN_TC_SHARED_LINKER_FLAGS) else CONAN_TC_LDFLAGS += $(CONAN_TC_EXE_LINKER_FLAGS) endif endif CONAN_TC_CPPFLAGS += $(addprefix -D,$(CONAN_TC_DEFINES)) # Call this function in your Makefile to have Conan variables added to the standard variables # Example: $(call CONAN_TC_SETUP) CONAN_TC_SETUP = \\ $(eval CFLAGS += $(CONAN_TC_CFLAGS)) ; \\ $(eval CXXFLAGS += $(CONAN_TC_CXXFLAGS)) ; \\ $(eval CPPFLAGS += $(CONAN_TC_CPPFLAGS)) ; \\ $(eval LDFLAGS += $(CONAN_TC_LDFLAGS)) ; """)) context = { "arch_host": conanfile.settings.get_safe("arch"), "arch_build": detected_architecture(), "compiler": conanfile.settings.get_safe("compiler"), "compiler_version": conanfile.settings.get_safe("compiler.version"), "arch_flag": architecture_flag(settings_mock), "cppstd_flag": cppstd_flag_new(conanfile.settings), "build_type_flags": " ".join(build_type_flags(conanfile.settings)), "build_type": build_type, "libcxx": libcxx, "set_shared": shared, "set_fpic": fpic, "preserved_space": " ", } # expected_content = expected_template.render(context) self.maxDiff = None self.assertIn(expected_content, content)
def _get_cmake_definitions(self): def add_cmake_flag(cmake_flags, name, flag): """ appends compiler linker flags (if already present), or just sets """ if flag: if name not in cmake_flags: cmake_flags[name] = flag else: cmake_flags[name] = ' ' + flag return cmake_flags ret = OrderedDict() ret.update(self._build_type_definition()) ret.update(self._runtime_definition()) ret.update(self._cmake_compiler_options()) ret.update(self._cmake_cross_build_defines()) ret.update(self._get_cpp_standard_vars()) ret["CONAN_EXPORTED"] = "1" if self._compiler: ret["CONAN_COMPILER"] = self._compiler if self._compiler_version: ret["CONAN_COMPILER_VERSION"] = str(self._compiler_version) # Force compiler flags -- TODO: give as environment/setting parameter? arch_flag = architecture_flag(compiler=self._compiler, arch=self._arch) ret = add_cmake_flag(ret, 'CONAN_CXX_FLAGS', arch_flag) ret = add_cmake_flag(ret, 'CONAN_SHARED_LINKER_FLAGS', arch_flag) ret = add_cmake_flag(ret, 'CONAN_C_FLAGS', arch_flag) if self._set_cmake_flags: ret = add_cmake_flag(ret, 'CMAKE_CXX_FLAGS', arch_flag) ret = add_cmake_flag(ret, 'CMAKE_SHARED_LINKER_FLAGS', arch_flag) ret = add_cmake_flag(ret, 'CMAKE_C_FLAGS', arch_flag) if self._libcxx: ret["CONAN_LIBCXX"] = self._libcxx # Shared library try: ret["BUILD_SHARED_LIBS"] = "ON" if self._conanfile.options.shared else "OFF" except ConanException: pass # Install to package folder try: if self._conanfile.package_folder: ret["CMAKE_INSTALL_PREFIX"] = self._conanfile.package_folder except AttributeError: pass if str(self._os) in ["Windows", "WindowsStore"] and self._compiler == "Visual Studio": if self.parallel: cpus = tools.cpu_count() ret["CONAN_CXX_FLAGS"] = "/MP%s" % cpus ret["CONAN_C_FLAGS"] = "/MP%s" % cpus # fpic 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") ret["CONAN_CMAKE_POSITION_INDEPENDENT_CODE"] = "ON" if (fpic or shared) else "OFF" # Adjust automatically the module path in case the conanfile is using the cmake_find_package if "cmake_find_package" in self._conanfile.generators: ret["CMAKE_MODULE_PATH"] = self._conanfile.install_folder.replace("\\", "/") return ret
def _get_cmake_definitions(self): def add_cmake_flag(cmake_flags, name, flag): """ appends compiler linker flags (if already present), or just sets """ if flag: if name not in cmake_flags: cmake_flags[name] = flag else: cmake_flags[name] = ' ' + flag return cmake_flags ret = OrderedDict() ret.update(self._build_type_definition()) ret.update(self._runtime_definition()) ret.update(self._cmake_compiler_options()) ret.update(self._cmake_cross_build_defines()) ret.update(self._get_cpp_standard_vars()) ret["CONAN_EXPORTED"] = "1" if self._compiler: ret["CONAN_COMPILER"] = self._compiler if self._compiler_version: ret["CONAN_COMPILER_VERSION"] = str(self._compiler_version) # Force compiler flags -- TODO: give as environment/setting parameter? arch_flag = architecture_flag(compiler=self._compiler, arch=self._arch) ret = add_cmake_flag(ret, 'CONAN_CXX_FLAGS', arch_flag) ret = add_cmake_flag(ret, 'CONAN_SHARED_LINKER_FLAGS', arch_flag) ret = add_cmake_flag(ret, 'CONAN_C_FLAGS', arch_flag) if self._set_cmake_flags: ret = add_cmake_flag(ret, 'CMAKE_CXX_FLAGS', arch_flag) ret = add_cmake_flag(ret, 'CMAKE_SHARED_LINKER_FLAGS', arch_flag) ret = add_cmake_flag(ret, 'CMAKE_C_FLAGS', arch_flag) if self._libcxx: ret["CONAN_LIBCXX"] = self._libcxx # Shared library try: ret["BUILD_SHARED_LIBS"] = "ON" if self._conanfile.options.shared else "OFF" except ConanException: pass # Install to package folder try: if self._conanfile.package_folder: ret["CMAKE_INSTALL_PREFIX"] = self._conanfile.package_folder except AttributeError: pass if str(self._os) in ["Windows", "WindowsStore"] and self._compiler == "Visual Studio": if self.parallel: cpus = tools.cpu_count() ret["CONAN_CXX_FLAGS"] = "/MP%s" % cpus ret["CONAN_C_FLAGS"] = "/MP%s" % cpus # fpic 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") ret["CONAN_CMAKE_POSITION_INDEPENDENT_CODE"] = "ON" if (fpic or shared) else "OFF" return ret
def content(self): """With compiler_args you can invoke your compiler: $ gcc main.c @conanbuildinfo.args -o main $ clang main.c @conanbuildinfo.args -o main $ cl /EHsc main.c @conanbuildinfo.args """ flags = [] flags.extend( format_defines(self._deps_build_info.defines, compiler=self.compiler)) flags.extend( format_include_paths(self._deps_build_info.include_paths, compiler=self.compiler)) flags.extend(self._deps_build_info.cppflags) flags.extend(self._deps_build_info.cflags) arch_flag = architecture_flag( arch=self.conanfile.settings.get_safe("arch"), compiler=self.compiler) if arch_flag: flags.append(arch_flag) build_type = self.conanfile.settings.get_safe("build_type") btf = build_type_flag(compiler=self.compiler, build_type=build_type) if btf: flags.append(btf) btd = build_type_define(build_type=build_type) if btd: flags.extend(format_defines([btd], self.compiler)) if self.compiler == "Visual Studio": runtime = visual_runtime( self.conanfile.settings.get_safe("compiler.runtime")) if runtime: flags.append(runtime) # Necessary in the "cl" invocation before specify the rest of linker flags flags.append(visual_linker_option_separator) the_os = self.conanfile.settings.get_safe("os_build") or \ self.conanfile.settings.get_safe("os") flags.extend( rpath_flags(the_os, self.compiler, self._deps_build_info.lib_paths)) flags.extend( format_library_paths(self._deps_build_info.lib_paths, compiler=self.compiler)) flags.extend( format_libraries(self._deps_build_info.libs, compiler=self.compiler)) flags.extend(self._deps_build_info.sharedlinkflags) flags.extend(self._deps_build_info.exelinkflags) flags.extend(self._libcxx_flags()) flags.extend( cppstd_flag(self.conanfile.settings.get_safe("compiler"), self.conanfile.settings.get_safe("compiler.version"), self.conanfile.settings.get_safe("cppstd"))) sysrf = sysroot_flag(self._deps_build_info.sysroot, compiler=self.compiler) if sysrf: flags.append(sysrf) return " ".join(flag for flag in flags if flag)
def _get_architecture(self): # This should be factorized and make it toolchain-private return architecture_flag(self._conanfile.settings)
def get_definitions(self): compiler = self._ss("compiler") compiler_version = self._ss("compiler.version") arch = self._ss("arch") os_ = self._ss("os") libcxx = self._ss("compiler.libcxx") runtime = self._ss("compiler.runtime") build_type = self._ss("build_type") ret = OrderedDict() ret.update(runtime_definition(runtime)) if self._forced_build_type and self._forced_build_type != build_type: self._output.warn("Forced CMake build type ('%s') different from the settings build " "type ('%s')" % (self._forced_build_type, build_type)) build_type = self._forced_build_type ret.update(build_type_definition(build_type, self._generator)) if str(os_) == "Macos": if arch == "x86": ret["CMAKE_OSX_ARCHITECTURES"] = "i386" ret.update(self._cmake_cross_build_defines()) ret.update(self._get_cpp_standard_vars()) ret["CONAN_EXPORTED"] = "1" ret.update(in_local_cache_definition(self._conanfile.in_local_cache)) if compiler: ret["CONAN_COMPILER"] = compiler if compiler_version: ret["CONAN_COMPILER_VERSION"] = str(compiler_version) # C, CXX, LINK FLAGS if compiler == "Visual Studio": if self._parallel: flag = parallel_compiler_cl_flag(output=self._output) ret['CONAN_CXX_FLAGS'] = flag ret['CONAN_C_FLAGS'] = flag else: # arch_flag is only set for non Visual Studio arch_flag = architecture_flag(compiler=compiler, arch=arch) if arch_flag: ret['CONAN_CXX_FLAGS'] = arch_flag ret['CONAN_SHARED_LINKER_FLAGS'] = arch_flag ret['CONAN_C_FLAGS'] = arch_flag if self._set_cmake_flags: ret['CMAKE_CXX_FLAGS'] = arch_flag ret['CMAKE_SHARED_LINKER_FLAGS'] = arch_flag ret['CMAKE_C_FLAGS'] = arch_flag if libcxx: ret["CONAN_LIBCXX"] = libcxx # Shared library try: ret["BUILD_SHARED_LIBS"] = "ON" if self._conanfile.options.shared else "OFF" except ConanException: pass # Install to package folder try: if self._conanfile.package_folder: ret["CMAKE_INSTALL_PREFIX"] = self._conanfile.package_folder ret["CMAKE_INSTALL_BINDIR"] = DEFAULT_BIN ret["CMAKE_INSTALL_SBINDIR"] = DEFAULT_BIN ret["CMAKE_INSTALL_LIBEXECDIR"] = DEFAULT_BIN ret["CMAKE_INSTALL_LIBDIR"] = DEFAULT_LIB ret["CMAKE_INSTALL_INCLUDEDIR"] = DEFAULT_INCLUDE ret["CMAKE_INSTALL_OLDINCLUDEDIR"] = DEFAULT_INCLUDE ret["CMAKE_INSTALL_DATAROOTDIR"] = DEFAULT_SHARE except AttributeError: pass # fpic if not str(os_).startswith("Windows"): fpic = self._conanfile.options.get_safe("fPIC") if fpic is not None: shared = self._conanfile.options.get_safe("shared") ret["CONAN_CMAKE_POSITION_INDEPENDENT_CODE"] = "ON" if (fpic or shared) else "OFF" # Adjust automatically the module path in case the conanfile is using the # cmake_find_package or cmake_find_package_multi install_folder = self._conanfile.install_folder.replace("\\", "/") if "cmake_find_package" in self._conanfile.generators: ret["CMAKE_MODULE_PATH"] = install_folder if "cmake_find_package_multi" in self._conanfile.generators: # The cmake_find_package_multi only works with targets and generates XXXConfig.cmake # that require the prefix path and the module path ret["CMAKE_PREFIX_PATH"] = install_folder ret["CMAKE_MODULE_PATH"] = install_folder ret.update(self._get_make_program_definition()) # Disable CMake export registry #3070 (CMake installing modules in user home's) ret["CMAKE_EXPORT_NO_PACKAGE_REGISTRY"] = "ON" return ret
def get_definitions(self, cmake_version): compiler = self._ss("compiler") compiler_base = self._ss("compiler.base") compiler_version = self._ss("compiler.version") arch = self._ss("arch") os_ = self._ss("os") libcxx = self._ss("compiler.libcxx") runtime = self._ss("compiler.runtime") build_type = self._ss("build_type") definitions = OrderedDict() definitions.update(runtime_definition(runtime)) definitions.update(build_type_definition(self._forced_build_type, build_type, self._generator, self._output)) # don't attempt to override variables set within toolchain if (tools.is_apple_os(os_) and "CONAN_CMAKE_TOOLCHAIN_FILE" not in os.environ and "CMAKE_TOOLCHAIN_FILE" not in definitions): apple_arch = tools.to_apple_arch(arch) if apple_arch: definitions["CMAKE_OSX_ARCHITECTURES"] = apple_arch # xcrun is only available on macOS, otherwise it's cross-compiling and it needs to be # set within CMake toolchain. also, if SDKROOT is set, CMake will use it, and it's not # needed to run xcrun. if platform.system() == "Darwin" and "SDKROOT" not in os.environ: sdk_path = tools.XCRun(self._conanfile.settings).sdk_path if sdk_path: definitions["CMAKE_OSX_SYSROOT"] = sdk_path definitions.update(self._cmake_cross_build_defines(cmake_version)) definitions.update(self._get_cpp_standard_vars()) definitions.update(in_local_cache_definition(self._conanfile.in_local_cache)) if compiler: definitions["CONAN_COMPILER"] = compiler if compiler_version: definitions["CONAN_COMPILER_VERSION"] = str(compiler_version) # C, CXX, LINK FLAGS if compiler == "Visual Studio" or compiler_base == "Visual Studio": if self._parallel: flag = parallel_compiler_cl_flag(output=self._output) definitions['CONAN_CXX_FLAGS'] = flag definitions['CONAN_C_FLAGS'] = flag else: # arch_flag is only set for non Visual Studio arch_flag = architecture_flag(self._conanfile.settings) if arch_flag: definitions['CONAN_CXX_FLAGS'] = arch_flag definitions['CONAN_SHARED_LINKER_FLAGS'] = arch_flag definitions['CONAN_C_FLAGS'] = arch_flag if self._set_cmake_flags: definitions['CMAKE_CXX_FLAGS'] = arch_flag definitions['CMAKE_SHARED_LINKER_FLAGS'] = arch_flag definitions['CMAKE_C_FLAGS'] = arch_flag if libcxx: definitions["CONAN_LIBCXX"] = libcxx # Shared library try: definitions["BUILD_SHARED_LIBS"] = "ON" if self._conanfile.options.shared else "OFF" except ConanException: pass # Install to package folder try: if self._conanfile.package_folder: definitions["CMAKE_INSTALL_PREFIX"] = self._conanfile.package_folder definitions["CMAKE_INSTALL_BINDIR"] = DEFAULT_BIN definitions["CMAKE_INSTALL_SBINDIR"] = DEFAULT_BIN definitions["CMAKE_INSTALL_LIBEXECDIR"] = DEFAULT_BIN definitions["CMAKE_INSTALL_LIBDIR"] = DEFAULT_LIB definitions["CMAKE_INSTALL_INCLUDEDIR"] = DEFAULT_INCLUDE definitions["CMAKE_INSTALL_OLDINCLUDEDIR"] = DEFAULT_INCLUDE definitions["CMAKE_INSTALL_DATAROOTDIR"] = DEFAULT_SHARE except AttributeError: pass # fpic if not str(os_).startswith("Windows"): fpic = self._conanfile.options.get_safe("fPIC") if fpic is not None: shared = self._conanfile.options.get_safe("shared") fpic_value = "ON" if (fpic or shared) else "OFF" definitions["CONAN_CMAKE_POSITION_INDEPENDENT_CODE"] = fpic_value # Adjust automatically the module path in case the conanfile is using the # cmake_find_package or cmake_find_package_multi install_folder = self._conanfile.install_folder.replace("\\", "/") if "cmake_find_package" in self._conanfile.generators: definitions["CMAKE_MODULE_PATH"] = install_folder if "cmake_find_package_multi" in self._conanfile.generators: # The cmake_find_package_multi only works with targets and generates XXXConfig.cmake # that require the prefix path and the module path definitions["CMAKE_PREFIX_PATH"] = install_folder definitions["CMAKE_MODULE_PATH"] = install_folder definitions.update(self._get_make_program_definition()) # Disable CMake export registry #3070 (CMake installing modules in user home's) definitions["CMAKE_EXPORT_NO_PACKAGE_REGISTRY"] = "ON" return definitions
def test_arch_flag(self, compiler, arch, os, flag): self.assertEqual(architecture_flag(compiler=compiler, arch=arch, os=os), flag)