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 test_build_type_flags(self, compiler, build_type, vs_toolset, flags): settings = MockSettings({ "compiler": compiler, "build_type": build_type, "compiler.toolset": vs_toolset }) self.assertEqual(' '.join(build_type_flags(settings)), flags)
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 vs_build_type_flags(settings): build_type = settings.get_safe("build_type") ret = [] btd = build_type_define(build_type=build_type) if btd: ret.extend(format_defines([btd])) btfs = build_type_flags("Visual Studio", build_type=build_type, vs_toolset=settings.get_safe("compiler.toolset")) if btfs: ret.extend(btfs) return ret
def vs_build_type_flags(settings, with_flags=True): build_type = settings.get_safe("build_type") ret = [] btd = build_type_define(build_type=build_type) if btd: ret.extend(format_defines([btd])) if with_flags: # When using to build a vs project we don't want to adjust these flags btfs = build_type_flags(settings) if btfs: ret.extend(btfs) 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)) 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) 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(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 __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_build_type_flags(self): flags = build_type_flags(compiler='Visual Studio', build_type='Debug') self.assertEqual(" ".join(flags), '-Zi -Ob0 -Od') flags = build_type_flags(compiler='Visual Studio', build_type='Release') self.assertEqual(" ".join(flags), "-O2 -Ob2") flags = build_type_flags(compiler='Visual Studio', build_type='RelWithDebInfo') self.assertEqual(" ".join(flags), '-Zi -O2 -Ob1') flags = build_type_flags(compiler='Visual Studio', build_type='MinSizeRel') self.assertEqual(" ".join(flags), '-O1 -Ob1') # With clang toolset flags = build_type_flags(compiler='Visual Studio', build_type='Debug', vs_toolset="v140_clang_c2") self.assertEqual(" ".join(flags), '-gline-tables-only -fno-inline -O0') flags = build_type_flags(compiler='Visual Studio', build_type='Release', vs_toolset="v140_clang_c2") self.assertEqual(" ".join(flags), "-O2") flags = build_type_flags(compiler='Visual Studio', build_type='RelWithDebInfo', vs_toolset="v140_clang_c2") self.assertEqual(" ".join(flags), '-gline-tables-only -O2 -fno-inline') flags = build_type_flags(compiler='Visual Studio', build_type='MinSizeRel', vs_toolset="v140_clang_c2") self.assertEqual(" ".join(flags), '') # GCC flags = build_type_flags(compiler='gcc', build_type='Debug') self.assertEqual(" ".join(flags), '-g') flags = build_type_flags(compiler='gcc', build_type='Release') self.assertEqual(" ".join(flags), '-O3 -s') flags = build_type_flags(compiler='gcc', build_type='RelWithDebInfo') self.assertEqual(" ".join(flags), '-O2 -g') flags = build_type_flags(compiler='gcc', build_type='MinSizeRel') self.assertEqual(" ".join(flags), '-Os') flags = build_type_flags(compiler='clang', build_type='Debug') self.assertEqual(" ".join(flags), '-g') flags = build_type_flags(compiler='clang', build_type='Release') self.assertEqual(" ".join(flags), '-O3') flags = build_type_flags(compiler='clang', build_type='RelWithDebInfo') self.assertEqual(" ".join(flags), '-O2 -g') flags = build_type_flags(compiler='clang', build_type='MinSizeRel') self.assertEqual(" ".join(flags), '-Os') # SUN CC flags = build_type_flags(compiler='sun-cc', build_type='Debug') self.assertEqual(" ".join(flags), '-g') flags = build_type_flags(compiler='sun-cc', build_type='Release') self.assertEqual(" ".join(flags), '-xO3') flags = build_type_flags(compiler='sun-cc', build_type='RelWithDebInfo') self.assertEqual(" ".join(flags), '-xO2 -g') flags = build_type_flags(compiler='sun-cc', build_type='MinSizeRel') self.assertEqual(" ".join(flags), '-xO2 -xspace') # Define define = build_type_define(build_type='Release') self.assertEqual(define, 'NDEBUG')
def test_build_type_flags(self): flags = build_type_flags(compiler='Visual Studio', build_type='Debug') self.assertEquals(" ".join(flags), '-Zi -Ob0 -Od') flags = build_type_flags(compiler='Visual Studio', build_type='Release') self.assertEquals(" ".join(flags), "-O2 -Ob2") flags = build_type_flags(compiler='Visual Studio', build_type='RelWithDebInfo') self.assertEquals(" ".join(flags), '-Zi -O2 -Ob1') flags = build_type_flags(compiler='Visual Studio', build_type='MinSizeRel') self.assertEquals(" ".join(flags), '-O1 -Ob1') # With clang toolset flags = build_type_flags(compiler='Visual Studio', build_type='Debug', vs_toolset="v140_clang_c2") self.assertEquals(" ".join(flags), '-gline-tables-only -fno-inline -O0') flags = build_type_flags(compiler='Visual Studio', build_type='Release', vs_toolset="v140_clang_c2") self.assertEquals(" ".join(flags), "-O2") flags = build_type_flags(compiler='Visual Studio', build_type='RelWithDebInfo', vs_toolset="v140_clang_c2") self.assertEquals(" ".join(flags), '-gline-tables-only -O2 -fno-inline') flags = build_type_flags(compiler='Visual Studio', build_type='MinSizeRel', vs_toolset="v140_clang_c2") self.assertEquals(" ".join(flags), '') # GCC flags = build_type_flags(compiler='gcc', build_type='Debug') self.assertEquals(" ".join(flags), '-g') flags = build_type_flags(compiler='gcc', build_type='Release') self.assertEquals(" ".join(flags), '-O3 -s') flags = build_type_flags(compiler='gcc', build_type='RelWithDebInfo') self.assertEquals(" ".join(flags), '-O2 -g') flags = build_type_flags(compiler='gcc', build_type='MinSizeRel') self.assertEquals(" ".join(flags), '-Os') flags = build_type_flags(compiler='clang', build_type='Debug') self.assertEquals(" ".join(flags), '-g') flags = build_type_flags(compiler='clang', build_type='Release') self.assertEquals(" ".join(flags), '-O3') flags = build_type_flags(compiler='clang', build_type='RelWithDebInfo') self.assertEquals(" ".join(flags), '-O2 -g') flags = build_type_flags(compiler='clang', build_type='MinSizeRel') self.assertEquals(" ".join(flags), '-Os') # SUN CC flags = build_type_flags(compiler='sun-cc', build_type='Debug') self.assertEquals(" ".join(flags), '-g') flags = build_type_flags(compiler='sun-cc', build_type='Release') self.assertEquals(" ".join(flags), '-xO3') flags = build_type_flags(compiler='sun-cc', build_type='RelWithDebInfo') self.assertEquals(" ".join(flags), '-xO2 -g') flags = build_type_flags(compiler='sun-cc', build_type='MinSizeRel') self.assertEquals(" ".join(flags), '-xO2 -xspace') # Define define = build_type_define(build_type='Release') self.assertEquals(define, 'NDEBUG')
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 test_build_type_flags(self, compiler, build_type, vs_toolset, flags): self.assertEqual(' '.join(build_type_flags(compiler=compiler, build_type=build_type, vs_toolset=vs_toolset)), flags)