Skip to content

Commit

Permalink
- handle compiler flags for Intel C++
Browse files Browse the repository at this point in the history
Signed-off-by: SSE4 <tomskside@gmail.com>
  • Loading branch information
SSE4 committed Apr 9, 2020
1 parent cf4cab6 commit 7f37807
Show file tree
Hide file tree
Showing 10 changed files with 199 additions and 112 deletions.
47 changes: 25 additions & 22 deletions conans/client/build/autotools_environment.py
Expand Up @@ -237,36 +237,35 @@ 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)
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, win_bash=self._win_bash,
subsystem=self.subsystem,
compiler=self._compiler)
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:
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))
ret.extend(rpath_flags(self._conanfile.settings, self._deps_cpp_info.lib_paths))

return ret

def _configure_flags(self):
ret = copy.copy(self._deps_cpp_info.cflags)
arch_flag = architecture_flag(compiler=self._compiler, os=self._os, arch=self._arch)
arch_flag = architecture_flag(self._conanfile.settings)
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"))
btfs = build_type_flags(self._conanfile.settings)
if btfs:
ret.extend(btfs)
srf = sysroot_flag(self._deps_cpp_info.sysroot, win_bash=self._win_bash,
subsystem=self.subsystem,
compiler=self._compiler)
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:
Expand All @@ -276,7 +275,7 @@ def _configure_flags(self):

def _configure_cxx_flags(self):
ret = copy.copy(self._deps_cpp_info.cxxflags)
cxxf = libcxx_flag(compiler=self._compiler, libcxx=self._libcxx)
cxxf = libcxx_flag(self._conanfile.settings)
if cxxf:
ret.append(cxxf)
return ret
Expand All @@ -291,7 +290,7 @@ def _configure_defines(self):
ret.append(btf)

# CXX11 ABI
abif = libcxx_define(compiler=self._compiler, libcxx=self._libcxx)
abif = libcxx_define(self._conanfile.settings)
if abif:
ret.append(abif)
return ret
Expand All @@ -307,18 +306,22 @@ def append(*args):
ret.append(arg)
return ret

lib_paths = format_library_paths(self.library_paths, win_bash=self._win_bash,
subsystem=self.subsystem, compiler=self._compiler)
include_paths = format_include_paths(self.include_paths, win_bash=self._win_bash,
subsystem=self.subsystem, compiler=self._compiler)
lib_paths = format_library_paths(self.library_paths,
self._conanfile.settings,
win_bash=self._win_bash,
subsystem=self.subsystem)
include_paths = format_include_paths(self.include_paths,
self._conanfile.settings,
win_bash=self._win_bash,
subsystem=self.subsystem)

ld_flags = append(self.link_flags, lib_paths)
cpp_flags = append(include_paths, format_defines(self.defines))
libs = format_libraries(self.libs, compiler=self._compiler)
libs = format_libraries(self.libs, self._conanfile.settings)

tmp_compilation_flags = copy.copy(self.flags)
if self.fpic:
tmp_compilation_flags.append(pic_flag(self._compiler))
tmp_compilation_flags.append(pic_flag(self._conanfile.settings))

cxx_flags = append(tmp_compilation_flags, self.cxx_flags, self.cppstd_flag)
c_flags = tmp_compilation_flags
Expand Down
2 changes: 1 addition & 1 deletion conans/client/build/cmake_flags.py
Expand Up @@ -301,7 +301,7 @@ def get_definitions(self):
definitions['CONAN_CXX_FLAGS'] = flag
definitions['CONAN_C_FLAGS'] = flag
else: # arch_flag is only set for non Visual Studio
arch_flag = architecture_flag(compiler=compiler, os=os_, arch=arch)
arch_flag = architecture_flag(self._conanfile.settings)
if arch_flag:
definitions['CONAN_CXX_FLAGS'] = arch_flag
definitions['CONAN_SHARED_LINKER_FLAGS'] = arch_flag
Expand Down
93 changes: 65 additions & 28 deletions conans/client/build/compiler_flags.py
Expand Up @@ -9,24 +9,39 @@
# -LIBPATH, -D, -I, -ZI and so on.
"""

from conans.client.tools.apple import is_apple_os
from conans.client.tools.oss import cpu_count
from conans.client.tools.win import unix_path


def rpath_flags(os_build, compiler, lib_paths):
GCC_LIKE = ['clang', 'apple-clang', 'gcc']


def _base_compiler(settings):
return settings.get_safe("compiler.base") or settings.get_safe("compiler")


def rpath_flags(settings, lib_paths):
os_build = settings.get_safe("os_build") or settings.get_safe("os")
compiler = _base_compiler(settings)
if not os_build:
return []
if compiler in ("clang", "apple-clang", "gcc"):
rpath_separator = "," if os_build in ["Macos", "iOS", "watchOS", "tvOS"] else "="
if compiler in GCC_LIKE:
rpath_separator = "," if is_apple_os(os_build) else "="
return ['-Wl,-rpath%s"%s"' % (rpath_separator, x.replace("\\", "/"))
for x in lib_paths if x]
return []


def architecture_flag(compiler, arch, os=None):
def architecture_flag(settings):
"""
returns flags specific to the target architecture and compiler
"""
compiler = settings.get_safe("compiler")
compiler_base = settings.get_safe("compiler.base")
arch = settings.get_safe("arch")
the_os = settings.get_safe("os")
if not compiler or not arch:
return ""

Expand All @@ -37,31 +52,40 @@ def architecture_flag(compiler, arch, os=None):
return '-m32'
elif str(arch) in ['s390']:
return '-m31'
elif os == 'AIX':
elif str(the_os) == 'AIX':
if str(arch) in ['ppc32']:
return '-maix32'
elif str(arch) in ['ppc64']:
return '-maix64'
elif str(compiler) == "intel":
# https://software.intel.com/en-us/cpp-compiler-developer-guide-and-reference-m32-m64-qm32-qm64
if str(arch) == "x86":
return "/Qm32" if str(compiler_base) == "Visual Studio" else "-m32"
elif str(arch) == "x86_64":
return "/Qm64" if str(compiler_base) == "Visual Studio" else "-m64"
return ""


def libcxx_define(compiler, libcxx):

def libcxx_define(settings):
compiler = _base_compiler(settings)
libcxx = settings.get_safe("compiler.libcxx")
if not compiler or not libcxx:
return ""

if str(compiler) in ['gcc', 'clang', 'apple-clang']:
if str(compiler) in GCC_LIKE:
if str(libcxx) == 'libstdc++':
return '_GLIBCXX_USE_CXX11_ABI=0'
elif str(libcxx) == 'libstdc++11':
return '_GLIBCXX_USE_CXX11_ABI=1'
return ""


def libcxx_flag(compiler, libcxx):
def libcxx_flag(settings):
"""
returns flag specific to the target C++ standard library
"""
compiler = _base_compiler(settings)
libcxx = settings.get_safe("compiler.libcxx")
if not compiler or not libcxx:
return ""
if str(compiler) in ['clang', 'apple-clang']:
Expand All @@ -79,20 +103,24 @@ def libcxx_flag(compiler, libcxx):
return ""


def pic_flag(compiler=None):
def pic_flag(settings):
"""
returns PIC (position independent code) flags, such as -fPIC
"""
compiler = _base_compiler(settings)
if not compiler or compiler == 'Visual Studio':
return ""
return '-fPIC'


def build_type_flags(compiler, build_type, vs_toolset=None):
def build_type_flags(settings):
"""
returns flags specific to the build type (Debug, Release, etc.)
(-s, -g, /Zi, etc.)
"""
compiler = _base_compiler(settings)
build_type = settings.get_safe("build_type")
vs_toolset = settings.get_safe("compiler.toolset")
if not compiler or not build_type:
return ""

Expand Down Expand Up @@ -144,16 +172,17 @@ def build_type_define(build_type=None):
returns definitions specific to the build type (Debug, Release, etc.)
like DEBUG, _DEBUG, NDEBUG
"""
return 'NDEBUG' if build_type == 'Release' else ""
return 'NDEBUG' if build_type in ['Release', 'RelWithDebInfo', 'MinSizeRel'] else ""


def adjust_path(path, win_bash=False, subsystem=None, compiler=None):
def adjust_path(path, settings, win_bash=False, subsystem=None):
"""
adjusts path to be safely passed to the compiler command line
for Windows bash, ensures path is in format according to the subsystem
for path with spaces, places double quotes around it
converts slashes to backslashes, or vice versa
"""
compiler = _base_compiler(settings)
if str(compiler) == 'Visual Studio':
path = path.replace('/', '\\')
else:
Expand All @@ -163,9 +192,10 @@ def adjust_path(path, win_bash=False, subsystem=None, compiler=None):
return '"%s"' % path if ' ' in path else path


def sysroot_flag(sysroot, win_bash=False, subsystem=None, compiler=None):
def sysroot_flag(sysroot, settings, win_bash=False, subsystem=None):
compiler = _base_compiler(settings)
if str(compiler) != 'Visual Studio' and sysroot:
sysroot = adjust_path(sysroot, win_bash=win_bash, subsystem=subsystem, compiler=compiler)
sysroot = adjust_path(sysroot, settings, win_bash=win_bash, subsystem=subsystem)
return '--sysroot=%s' % sysroot
return ""

Expand All @@ -184,23 +214,26 @@ def format_defines(defines):
visual_linker_option_separator = "-link" # Further options will apply to the linker


def format_include_paths(include_paths, win_bash=False, subsystem=None, compiler=None):
return ["%s%s" % (include_path_option, adjust_path(include_path, win_bash=win_bash,
subsystem=subsystem, compiler=compiler))
def format_include_paths(include_paths, settings, win_bash=False, subsystem=None):
return ["%s%s" % (include_path_option, adjust_path(include_path, settings, win_bash=win_bash,
subsystem=subsystem))
for include_path in include_paths if include_path]


def format_library_paths(library_paths, win_bash=False, subsystem=None, compiler=None):
def format_library_paths(library_paths, settings, win_bash=False, subsystem=None):
compiler = _base_compiler(settings)
pattern = "-LIBPATH:%s" if str(compiler) == 'Visual Studio' else "-L%s"
return [pattern % adjust_path(library_path, win_bash=win_bash,
subsystem=subsystem, compiler=compiler)
return [pattern % adjust_path(library_path, settings, win_bash=win_bash,
subsystem=subsystem)
for library_path in library_paths if library_path]


def format_libraries(libraries, compiler=None):
def format_libraries(libraries, settings):
result = []
compiler = settings.get_safe("compiler")
compiler_base = settings.get_safe("compiler.base")
for library in libraries:
if str(compiler) == 'Visual Studio':
if str(compiler) == 'Visual Studio' or str(compiler_base) == 'Visual Studio':
if not library.endswith(".lib"):
library += ".lib"
result.append(library)
Expand All @@ -213,21 +246,25 @@ def parallel_compiler_cl_flag(output=None):
return "/MP%s" % cpu_count(output=output)


def format_frameworks(frameworks, compiler=None):
def format_frameworks(frameworks, settings):
"""
returns an appropriate compiler flags to link with Apple Frameworks
or an empty array, if Apple Frameworks aren't supported by the given compiler
"""
if str(compiler) not in ["clang", "gcc", "apple-clang"]:
compiler = settings.get_safe("compiler")
compiler_base = settings.get_safe("compiler.base")
if (str(compiler) not in GCC_LIKE) and (str(compiler_base) not in GCC_LIKE):
return []
return ["-framework %s" % framework for framework in frameworks]


def format_framework_paths(framework_paths, compiler=None):
def format_framework_paths(framework_paths, settings):
"""
returns an appropriate compiler flags to specify Apple Frameworks search paths
or an empty array, if Apple Frameworks aren't supported by the given compiler
"""
if str(compiler) not in ["clang", "gcc", "apple-clang"]:
compiler = settings.get_safe("compiler")
compiler_base = settings.get_safe("compiler.base")
if (str(compiler) not in GCC_LIKE) and (str(compiler_base) not in GCC_LIKE):
return []
return ["-F %s" % adjust_path(framework_path) for framework_path in framework_paths]
return ["-F %s" % adjust_path(framework_path, settings) for framework_path in framework_paths]
3 changes: 1 addition & 2 deletions conans/client/build/visual_environment.py
Expand Up @@ -139,8 +139,7 @@ def vs_build_type_flags(settings, with_flags=True):
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("Visual Studio", build_type=build_type,
vs_toolset=settings.get_safe("compiler.toolset"))
btfs = build_type_flags(settings)
if btfs:
ret.extend(btfs)

Expand Down

0 comments on commit 7f37807

Please sign in to comment.