2021-01-29 01:33:14 +01:00
|
|
|
# encoding: utf-8
|
|
|
|
# compiler_optimizations.py -- main entry point for configuring C/C++ compilers
|
|
|
|
# Copyright (C) 2021 a1batross
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
|
|
|
|
try: from fwgslib import get_flags_by_type, get_flags_by_compiler
|
|
|
|
except: from waflib.extras.fwgslib import get_flags_by_type, get_flags_by_compiler
|
|
|
|
from waflib.Configure import conf
|
|
|
|
from waflib import Logs
|
|
|
|
|
|
|
|
'''
|
|
|
|
Flags can be overriden and new types can be added
|
|
|
|
by importing this as normal Python module
|
|
|
|
|
|
|
|
Example:
|
|
|
|
#!/usr/bin/env python
|
|
|
|
from waflib.extras import compiler_optimizations
|
|
|
|
|
|
|
|
compiler_optimizations.VALID_BUILD_TYPES += 'gottagofast'
|
|
|
|
compiler_optimizations.CFLAGS['gottagofast'] = {
|
|
|
|
'gcc': ['-Ogentoo']
|
|
|
|
}
|
|
|
|
'''
|
|
|
|
|
2023-11-02 12:32:39 +01:00
|
|
|
VALID_BUILD_TYPES = ['fastnative', 'fast', 'humanrights', 'debug', 'sanitize', 'msan', 'none']
|
2021-01-29 01:33:14 +01:00
|
|
|
|
|
|
|
LINKFLAGS = {
|
|
|
|
'common': {
|
|
|
|
'msvc': ['/DEBUG'], # always create PDB, doesn't affect result binaries
|
|
|
|
'gcc': ['-Wl,--no-undefined'],
|
|
|
|
'owcc': ['-Wl,option stack=512k']
|
|
|
|
},
|
2023-01-31 02:28:08 +01:00
|
|
|
'msan': {
|
|
|
|
'clang': ['-fsanitize=memory', '-pthread'],
|
|
|
|
'default': ['NO_MSAN_HERE']
|
|
|
|
},
|
2021-01-29 01:33:14 +01:00
|
|
|
'sanitize': {
|
2021-09-30 14:58:45 +02:00
|
|
|
'clang': ['-fsanitize=undefined', '-fsanitize=address', '-pthread'],
|
|
|
|
'gcc': ['-fsanitize=undefined', '-fsanitize=address', '-pthread'],
|
2022-01-15 20:41:33 +01:00
|
|
|
'msvc': ['/SAFESEH:NO']
|
2021-04-30 09:07:02 +02:00
|
|
|
},
|
|
|
|
'debug': {
|
|
|
|
'msvc': ['/INCREMENTAL', '/SAFESEH:NO']
|
2021-01-29 01:33:14 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
CFLAGS = {
|
|
|
|
'common': {
|
|
|
|
# disable thread-safe local static initialization for C++11 code, as it cause crashes on Windows XP
|
2022-08-01 02:11:13 +02:00
|
|
|
'msvc': ['/D_USING_V110_SDK71_', '/FS', '/Zc:threadSafeInit-', '/MT', '/MP', '/Zc:__cplusplus'],
|
2021-12-07 07:03:51 +01:00
|
|
|
'clang': ['-g', '-gdwarf-2', '-fvisibility=hidden', '-fno-threadsafe-statics'],
|
2021-01-29 01:33:14 +01:00
|
|
|
'gcc': ['-g', '-fvisibility=hidden'],
|
|
|
|
'owcc': ['-fno-short-enum', '-ffloat-store', '-g3']
|
|
|
|
},
|
|
|
|
'fast': {
|
2021-04-30 09:07:02 +02:00
|
|
|
'msvc': ['/O2', '/Oy', '/Zi'],
|
2021-01-29 01:33:14 +01:00
|
|
|
'gcc': {
|
|
|
|
'3': ['-O3', '-fomit-frame-pointer'],
|
2023-10-28 22:28:56 +02:00
|
|
|
'4': ['-Ofast', '-funsafe-math-optimizations', '-funsafe-loop-optimizations', '-fomit-frame-pointer'],
|
2023-05-14 08:34:55 +02:00
|
|
|
'default': ['-Ofast', '-funsafe-math-optimizations', '-funsafe-loop-optimizations', '-fomit-frame-pointer', '-fno-semantic-interposition']
|
2021-01-29 01:33:14 +01:00
|
|
|
},
|
|
|
|
'clang': ['-Ofast'],
|
|
|
|
'default': ['-O3']
|
|
|
|
},
|
|
|
|
'fastnative': {
|
2021-04-30 09:07:02 +02:00
|
|
|
'msvc': ['/O2', '/Oy', '/Zi'],
|
2023-05-14 08:34:55 +02:00
|
|
|
'gcc': ['-Ofast', '-march=native', '-funsafe-math-optimizations', '-funsafe-loop-optimizations', '-fomit-frame-pointer', '-fno-semantic-interposition'],
|
2021-01-29 01:33:14 +01:00
|
|
|
'clang': ['-Ofast', '-march=native'],
|
|
|
|
'default': ['-O3']
|
|
|
|
},
|
2023-11-02 12:32:39 +01:00
|
|
|
'humanrights': {
|
2021-04-30 09:07:02 +02:00
|
|
|
'msvc': ['/O2', '/Zi'],
|
2021-01-29 01:33:14 +01:00
|
|
|
'owcc': ['-O3', '-foptimize-sibling-calls', '-fomit-leaf-frame-pointer', '-fomit-frame-pointer', '-fschedule-insns', '-funsafe-math-optimizations', '-funroll-loops', '-frerun-optimizer', '-finline-functions', '-finline-limit=512', '-fguess-branch-probability', '-fno-strict-aliasing', '-floop-optimize'],
|
2023-12-01 05:07:51 +01:00
|
|
|
'gcc': {
|
|
|
|
'4': ['-O3'],
|
|
|
|
'3': ['-O3'],
|
|
|
|
'default': ['-O3','-fno-semantic-interposition'],
|
|
|
|
},
|
2021-01-29 01:33:14 +01:00
|
|
|
'default': ['-O3']
|
|
|
|
},
|
|
|
|
'debug': {
|
2021-04-30 09:07:02 +02:00
|
|
|
'msvc': ['/Od', '/ZI'],
|
2021-01-29 01:33:14 +01:00
|
|
|
'owcc': ['-O0', '-fno-omit-frame-pointer', '-funwind-tables', '-fno-omit-leaf-frame-pointer'],
|
2021-03-11 18:17:39 +01:00
|
|
|
'default': ['-O0']
|
2021-01-29 01:33:14 +01:00
|
|
|
},
|
2023-01-31 02:28:08 +01:00
|
|
|
'msan': {
|
|
|
|
'clang': ['-O2', '-g', '-fno-omit-frame-pointer', '-fsanitize=memory', '-pthread'],
|
|
|
|
'default': ['NO_MSAN_HERE']
|
|
|
|
},
|
2021-01-29 01:33:14 +01:00
|
|
|
'sanitize': {
|
2022-01-15 20:41:33 +01:00
|
|
|
'msvc': ['/Od', '/RTC1', '/Zi', '/fsanitize=address'],
|
2022-01-03 23:58:45 +01:00
|
|
|
'gcc': ['-O0', '-fsanitize=undefined', '-fsanitize=address', '-pthread'],
|
2021-09-30 14:58:45 +02:00
|
|
|
'clang': ['-O0', '-fsanitize=undefined', '-fsanitize=address', '-pthread'],
|
2021-01-29 01:33:14 +01:00
|
|
|
'default': ['-O0']
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
LTO_CFLAGS = {
|
|
|
|
'msvc': ['/GL'],
|
2023-05-14 08:34:29 +02:00
|
|
|
'gcc': ['-flto=auto'],
|
2021-01-29 01:33:14 +01:00
|
|
|
'clang': ['-flto']
|
|
|
|
}
|
|
|
|
|
|
|
|
LTO_LINKFLAGS = {
|
|
|
|
'msvc': ['/LTCG'],
|
2023-05-14 08:34:29 +02:00
|
|
|
'gcc': ['-flto=auto'],
|
2021-01-29 01:33:14 +01:00
|
|
|
'clang': ['-flto']
|
|
|
|
}
|
|
|
|
|
|
|
|
POLLY_CFLAGS = {
|
|
|
|
'gcc': ['-fgraphite-identity'],
|
|
|
|
'clang': ['-mllvm', '-polly']
|
|
|
|
# msvc sosat :(
|
|
|
|
}
|
|
|
|
|
2024-02-21 00:57:07 +01:00
|
|
|
PROFILE_GENERATE_CFLAGS = {
|
|
|
|
'gcc': ['-fprofile-generate=xash3d-prof'],
|
|
|
|
}
|
|
|
|
|
|
|
|
PROFILE_GENERATE_LINKFLAGS = {
|
|
|
|
'gcc': ['-fprofile-generate=xash3d-prof'],
|
|
|
|
}
|
|
|
|
|
|
|
|
PROFILE_USE_CFLAGS = {
|
|
|
|
'gcc': ['-fprofile-use=%s'],
|
|
|
|
}
|
|
|
|
|
|
|
|
PROFILE_USE_LINKFLAGS = {
|
|
|
|
'gcc': ['-fprofile-use=%s'],
|
|
|
|
}
|
|
|
|
|
2021-01-29 01:33:14 +01:00
|
|
|
def options(opt):
|
|
|
|
grp = opt.add_option_group('Compiler optimization options')
|
|
|
|
|
2023-11-02 12:32:39 +01:00
|
|
|
grp.add_option('-T', '--build-type', action='store', dest='BUILD_TYPE', default='humanrights',
|
2021-01-29 01:33:14 +01:00
|
|
|
help = 'build type: debug, release or none(custom flags)')
|
|
|
|
|
|
|
|
grp.add_option('--enable-lto', action = 'store_true', dest = 'LTO', default = False,
|
|
|
|
help = 'enable Link Time Optimization if possible [default: %default]')
|
|
|
|
|
|
|
|
grp.add_option('--enable-poly-opt', action = 'store_true', dest = 'POLLY', default = False,
|
|
|
|
help = 'enable polyhedral optimization if possible [default: %default]')
|
|
|
|
|
2024-02-21 00:57:07 +01:00
|
|
|
grp.add_option('--enable-profile', action = 'store_true', dest = 'PROFILE_GENERATE', default = False,
|
|
|
|
help = 'enable profile generating build (stored in xash3d-prof directory) [default: %default]')
|
|
|
|
|
|
|
|
grp.add_option('--use-profile', action = 'store', dest = 'PROFILE_USE', default = None,
|
|
|
|
help = 'use profile during build [default: %default]')
|
|
|
|
|
2021-01-29 01:33:14 +01:00
|
|
|
def configure(conf):
|
|
|
|
conf.start_msg('Build type')
|
2023-04-13 15:14:04 +02:00
|
|
|
|
2023-11-02 12:32:39 +01:00
|
|
|
# legacy naming for default release build
|
|
|
|
# https://chaos.social/@karolherbst/111340511652012860
|
|
|
|
if conf.options.BUILD_TYPE == 'release':
|
|
|
|
conf.options.BUILD_TYPE = 'humanrights'
|
|
|
|
|
2023-04-13 15:14:04 +02:00
|
|
|
if not conf.options.BUILD_TYPE in VALID_BUILD_TYPES:
|
2021-01-29 01:33:14 +01:00
|
|
|
conf.end_msg(conf.options.BUILD_TYPE, color='RED')
|
|
|
|
conf.fatal('Invalid build type. Valid are: %s' % ', '.join(VALID_BUILD_TYPES))
|
2023-04-13 15:14:04 +02:00
|
|
|
|
2021-01-29 01:33:14 +01:00
|
|
|
conf.end_msg(conf.options.BUILD_TYPE)
|
|
|
|
|
|
|
|
conf.msg('LTO build', 'yes' if conf.options.LTO else 'no')
|
|
|
|
conf.msg('PolyOpt build', 'yes' if conf.options.POLLY else 'no')
|
2024-02-21 00:57:07 +01:00
|
|
|
conf.msg('Generate profile', 'yes' if conf.options.PROFILE_GENERATE else 'no')
|
|
|
|
conf.msg('Use profile', conf.options.PROFILE_USE if not conf.options.PROFILE_GENERATE else 'no')
|
2021-01-29 01:33:14 +01:00
|
|
|
|
|
|
|
# -march=native should not be used
|
|
|
|
if conf.options.BUILD_TYPE.startswith('fast'):
|
|
|
|
Logs.warn('WARNING: \'%s\' build type should not be used in release builds', conf.options.BUILD_TYPE)
|
|
|
|
|
|
|
|
try:
|
|
|
|
conf.env.CC_VERSION[0]
|
|
|
|
except IndexError:
|
|
|
|
conf.env.CC_VERSION = (0,)
|
|
|
|
|
|
|
|
@conf
|
|
|
|
def get_optimization_flags(conf):
|
|
|
|
'''Returns a list of compile flags,
|
|
|
|
depending on build type and options set by user
|
|
|
|
|
|
|
|
NOTE: it doesn't filter out unsupported flags
|
|
|
|
|
|
|
|
:returns: tuple of cflags and linkflags
|
|
|
|
'''
|
|
|
|
linkflags = conf.get_flags_by_type(LINKFLAGS, conf.options.BUILD_TYPE, conf.env.COMPILER_CC, conf.env.CC_VERSION[0])
|
|
|
|
|
|
|
|
cflags = conf.get_flags_by_type(CFLAGS, conf.options.BUILD_TYPE, conf.env.COMPILER_CC, conf.env.CC_VERSION[0])
|
|
|
|
|
|
|
|
if conf.options.LTO:
|
|
|
|
linkflags+= conf.get_flags_by_compiler(LTO_LINKFLAGS, conf.env.COMPILER_CC)
|
|
|
|
cflags += conf.get_flags_by_compiler(LTO_CFLAGS, conf.env.COMPILER_CC)
|
|
|
|
|
|
|
|
if conf.options.POLLY:
|
|
|
|
cflags += conf.get_flags_by_compiler(POLLY_CFLAGS, conf.env.COMPILER_CC)
|
|
|
|
|
2024-02-21 00:57:07 +01:00
|
|
|
if conf.options.PROFILE_GENERATE:
|
|
|
|
linkflags+= conf.get_flags_by_compiler(PROFILE_GENERATE_LINKFLAGS, conf.env.COMPILER_CC)
|
|
|
|
cflags += conf.get_flags_by_compiler(PROFILE_GENERATE_CFLAGS, conf.env.COMPILER_CC)
|
|
|
|
elif conf.options.PROFILE_USE:
|
|
|
|
linkflags+= [conf.get_flags_by_compiler(PROFILE_USE_LINKFLAGS, conf.env.COMPILER_CC)[0] % conf.options.PROFILE_USE]
|
|
|
|
cflags += [conf.get_flags_by_compiler(PROFILE_USE_CFLAGS, conf.env.COMPILER_CC)[0] % conf.options.PROFILE_USE]
|
|
|
|
|
2023-02-05 02:09:32 +01:00
|
|
|
if conf.env.DEST_OS == 'nswitch' and conf.options.BUILD_TYPE == 'debug':
|
|
|
|
# enable remote debugger
|
|
|
|
cflags.append('-DNSWITCH_DEBUG')
|
2023-02-13 20:53:17 +01:00
|
|
|
elif conf.env.DEST_OS == 'psvita':
|
|
|
|
# this optimization is broken in vitasdk
|
|
|
|
cflags.append('-fno-optimize-sibling-calls')
|
|
|
|
# remove fvisibility to allow everything to be exported by default
|
|
|
|
cflags.remove('-fvisibility=hidden')
|
2023-02-05 02:09:32 +01:00
|
|
|
|
2023-05-24 03:19:08 +02:00
|
|
|
if conf.env.COMPILER_CC != 'msvc' and conf.env.COMPILER_CC != 'owcc':
|
|
|
|
# HLSDK by default compiles with these options under Linux
|
|
|
|
# no reason for us to not do the same
|
2023-05-24 03:30:28 +02:00
|
|
|
|
|
|
|
# TODO: fix DEST_CPU in force 32 bit mode
|
|
|
|
if conf.env.DEST_CPU == 'x86' or (conf.env.DEST_CPU == 'x86_64' and conf.env.DEST_SIZEOF_VOID_P == 4):
|
2023-05-24 03:19:08 +02:00
|
|
|
cflags.append('-march=pentium-m')
|
|
|
|
cflags.append('-mtune=core2')
|
|
|
|
|
2023-05-14 08:34:29 +02:00
|
|
|
# on all compilers (except MSVC?) we need to copy CFLAGS to LINKFLAGS
|
|
|
|
if conf.options.LTO and conf.env.COMPILER_CC != 'msvc':
|
|
|
|
linkflags += cflags
|
|
|
|
|
2021-01-29 01:33:14 +01:00
|
|
|
return cflags, linkflags
|