diff --git a/scripts/waifulib/compiler_optimizations.py b/scripts/waifulib/compiler_optimizations.py new file mode 100644 index 00000000..688d803b --- /dev/null +++ b/scripts/waifulib/compiler_optimizations.py @@ -0,0 +1,165 @@ +# 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'] +} +''' + +VALID_BUILD_TYPES = ['fastnative', 'fast', 'release', 'debug', 'nooptimize', 'sanitize', 'none'] + +LINKFLAGS = { + 'common': { + 'msvc': ['/DEBUG'], # always create PDB, doesn't affect result binaries + 'gcc': ['-Wl,--no-undefined'], + 'owcc': ['-Wl,option stack=512k'] + }, + 'sanitize': { + 'clang': ['-fsanitize=undefined', '-fsanitize=address'], + 'gcc': ['-fsanitize=undefined', '-fsanitize=address'], + } +} + +CFLAGS = { + 'common': { + # disable thread-safe local static initialization for C++11 code, as it cause crashes on Windows XP + 'msvc': ['/D_USING_V110_SDK71_', '/Zi', '/FS', '/Zc:threadSafeInit-', '/MT'], + 'clang': ['-g', '-gdwarf-2', '-fvisibility=hidden'], + 'gcc': ['-g', '-fvisibility=hidden'], + 'owcc': ['-fno-short-enum', '-ffloat-store', '-g3'] + }, + 'fast': { + 'msvc': ['/O2', '/Oy'], + 'gcc': { + '3': ['-O3', '-fomit-frame-pointer'], + 'default': ['-Ofast', '-funsafe-math-optimizations', '-funsafe-loop-optimizations', '-fomit-frame-pointer'] + }, + 'clang': ['-Ofast'], + 'default': ['-O3'] + }, + 'fastnative': { + 'msvc': ['/O2', '/Oy'], + 'gcc': ['-Ofast', '-march=native', '-funsafe-math-optimizations', '-funsafe-loop-optimizations', '-fomit-frame-pointer'], + 'clang': ['-Ofast', '-march=native'], + 'default': ['-O3'] + }, + 'release': { + 'msvc': ['/O2'], + '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'], + 'default': ['-O3'] + }, + 'debug': { + 'msvc': ['/O1'], + 'gcc': ['-Og'], + 'owcc': ['-O0', '-fno-omit-frame-pointer', '-funwind-tables', '-fno-omit-leaf-frame-pointer'], + 'default': ['-O1'] + }, + 'sanitize': { + 'msvc': ['/Od', '/RTC1'], + 'gcc': ['-Og', '-fsanitize=undefined', '-fsanitize=address'], + 'clang': ['-O0', '-fsanitize=undefined', '-fsanitize=address'], + 'default': ['-O0'] + }, + 'nooptimize': { + 'msvc': ['/Od'], + 'default': ['-O0'] + } +} + +LTO_CFLAGS = { + 'msvc': ['/GL'], + 'gcc': ['-flto'], + 'clang': ['-flto'] +} + +LTO_LINKFLAGS = { + 'msvc': ['/LTCG'], + 'gcc': ['-flto'], + 'clang': ['-flto'] +} + +POLLY_CFLAGS = { + 'gcc': ['-fgraphite-identity'], + 'clang': ['-mllvm', '-polly'] + # msvc sosat :( +} + +def options(opt): + grp = opt.add_option_group('Compiler optimization options') + + grp.add_option('-T', '--build-type', action='store', dest='BUILD_TYPE', default=None, + 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]') + +def configure(conf): + conf.start_msg('Build type') + if conf.options.BUILD_TYPE == None: + conf.end_msg('not set', color='RED') + conf.fatal('Set a build type, for example "-T release"') + elif not conf.options.BUILD_TYPE in VALID_BUILD_TYPES: + conf.end_msg(conf.options.BUILD_TYPE, color='RED') + conf.fatal('Invalid build type. Valid are: %s' % ', '.join(VALID_BUILD_TYPES)) + 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') + + # -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) + + return cflags, linkflags diff --git a/scripts/waifulib/enforce_pic.py b/scripts/waifulib/enforce_pic.py new file mode 100644 index 00000000..9c2c7e11 --- /dev/null +++ b/scripts/waifulib/enforce_pic.py @@ -0,0 +1,37 @@ +# encoding: utf-8 +# enforce_pic.py -- enforcing PIC if requested +# 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. + +from waflib.Configure import conf + +def options(opt): + pass + +def configure(conf): + pass + +@conf +def check_pic(conf, enable): + if enable: + # Every static library must have fPIC + if conf.env.DEST_OS != 'win32' and '-fPIC' in conf.env.CFLAGS_cshlib: + conf.env.append_unique('CFLAGS_cstlib', '-fPIC') + conf.env.append_unique('CXXFLAGS_cxxstlib', '-fPIC') + else: + if '-fPIC' in conf.env.CFLAGS_cshlib: + conf.env.CFLAGS_cshlib.remove('-fPIC') + if '-fPIC' in conf.env.CXXFLAGS_cshlib: + conf.env.CXXFLAGS_cxxshlib.remove('-fPIC') + if '-fPIC' in conf.env.CFLAGS_MACBUNDLE: + conf.env.CFLAGS_MACBUNDLE.remove('-fPIC') + if '-fPIC' in conf.env.CXXFLAGS_MACBUNDLE: + conf.env.CXXFLAGS_MACBUNDLE.remove('-fPIC') diff --git a/scripts/waifulib/xcompile.py b/scripts/waifulib/xcompile.py index 329cf51b..86c343fd 100644 --- a/scripts/waifulib/xcompile.py +++ b/scripts/waifulib/xcompile.py @@ -327,6 +327,10 @@ def options(opt): android.add_option('--android', action='store', dest='ANDROID_OPTS', default=None, help='enable building for android, format: --android=,,, example: --android=armeabi-v7a-hard,4.9,9') + magx = opt.add_option_group('MotoMAGX options') + magx.add_option('--enable-magx', action = 'store_true', dest = 'MAGX', default = False, + help = 'enable targetting for MotoMAGX phones [default: %default]') + def configure(conf): if conf.options.ANDROID_OPTS: values = conf.options.ANDROID_OPTS.split(',') @@ -362,7 +366,16 @@ def configure(conf): # conf.env.ANDROID_OPTS = android conf.env.DEST_OS2 = 'android' - + elif conf.options.MAGX: + # useless to change toolchain path, as toolchain meant to be placed in this path + toolchain_path = '/opt/toolchains/motomagx/arm-eabi2/lib/' + conf.env.INCLUDES_MAGX = [toolchain_path + i for i in ['ezx-z6/include', 'qt-2.3.8/include']] + conf.env.LIBPATH_MAGX = [toolchain_path + i for i in ['ezx-z6/lib', 'qt-2.3.8/lib']] + conf.env.LINKFLAGS_MAGX = ['-Wl,-rpath-link=' + i for i in conf.env.LIBPATH_MAGX] + for lib in ['qte-mt', 'ezxappbase', 'ezxpm', 'log_util']: + conf.check_cc(lib=lib, use='MAGX', uselib_store='MAGX') + + conf.env.MAGX = conf.options.MAGX MACRO_TO_DESTOS = OrderedDict({ '__ANDROID__' : 'android' }) for k in c_config.MACRO_TO_DESTOS: MACRO_TO_DESTOS[k] = c_config.MACRO_TO_DESTOS[k] # ordering is important diff --git a/wscript b/wscript index ef88b5fb..317e39d8 100644 --- a/wscript +++ b/wscript @@ -64,9 +64,6 @@ def subdirs(): def options(opt): grp = opt.add_option_group('Common options') - grp.add_option('-T', '--build-type', action='store', dest='BUILD_TYPE', default = None, - help = 'build type: debug, release or none(custom flags)') - grp.add_option('-d', '--dedicated', action = 'store_true', dest = 'DEDICATED', default = False, help = 'build Xash Dedicated Server [default: %default]') @@ -82,22 +79,13 @@ def options(opt): grp.add_option('--enable-bsp2', action = 'store_true', dest = 'SUPPORT_BSP2_FORMAT', default = False, help = 'build engine and renderers with BSP2 map support(recommended for Quake, breaks compatibility!) [default: %default]') - 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]') - grp.add_option('--low-memory-mode', action = 'store', dest = 'LOW_MEMORY', default = 0, type = 'int', help = 'enable low memory mode (only for devices have <128 ram)') - grp.add_option('--enable-magx', action = 'store_true', dest = 'MAGX', default = False, - help = 'enable targetting for MotoMAGX phones [default: %default]') - grp.add_option('--ignore-projects', action = 'store', dest = 'IGNORE_PROJECTS', default = None, help = 'disable selected projects from build [default: %default]') - opt.load('subproject') + opt.load('compiler_optimizations subproject') for i in SUBDIRS: if not i.mandatory and not opt.path.find_node(i.name+'/wscript'): @@ -106,31 +94,16 @@ def options(opt): opt.add_subproject(i.name) - opt.load('xshlib xcompile compiler_cxx compiler_c sdl2 clang_compilation_database strip_on_install waf_unit_test') if sys.platform == 'win32': opt.load('msvc msdev msvs') opt.load('reconfigure') def configure(conf): - enforce_pic = True # modern defaults - valid_build_types = ['fastnative', 'fast', 'release', 'debug', 'nooptimize', 'sanitize', 'none'] - conf.load('fwgslib reconfigure') + conf.load('fwgslib reconfigure compiler_optimizations') if conf.options.IGNORE_PROJECTS: conf.env.IGNORE_PROJECTS = conf.options.IGNORE_PROJECTS.split(',') - conf.start_msg('Build type') - if conf.options.BUILD_TYPE == None: - conf.end_msg('not set', color='RED') - conf.fatal('Please set a build type, for example "-T release"') - elif not conf.options.BUILD_TYPE in valid_build_types: - conf.end_msg(conf.options.BUILD_TYPE, color='RED') - conf.fatal('Invalid build type. Valid are: %s' % ', '.join(valid_build_types)) - conf.end_msg(conf.options.BUILD_TYPE) - - # -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) # Force XP compability, all build targets should add # subsystem=bld.env.MSVC_SUBSYSTEM @@ -139,12 +112,10 @@ def configure(conf): conf.env.MSVC_TARGETS = ['x86'] # explicitly request x86 target for MSVC if sys.platform == 'win32': conf.load('msvc msvc_pdb msdev msvs') - conf.load('xshlib subproject xcompile compiler_c compiler_cxx gitversion clang_compilation_database strip_on_install waf_unit_test') + conf.load('xshlib subproject xcompile compiler_c compiler_cxx gitversion clang_compilation_database strip_on_install waf_unit_test enforce_pic') + - try: - conf.env.CC_VERSION[0] - except IndexError: - conf.env.CC_VERSION = (0,) + enforce_pic = True # modern defaults # modify options dictionary early if conf.env.DEST_OS == 'android': @@ -152,44 +123,21 @@ def configure(conf): conf.options.NANOGL = True conf.options.GLWES = True conf.options.GL = False - - if conf.env.STATIC_LINKING: - enforce_pic = False # PIC may break static linking - - conf.env.MAGX = conf.options.MAGX - if conf.options.MAGX: - conf.options.USE_SELECT = True - conf.options.SDL12 = True - conf.options.NO_VGUI = True - conf.options.GL = False - conf.options.LOW_MEMORY = 1 - conf.options.SINGLE_BINARY = True + elif conf.env.MAGX: + conf.options.USE_SELECT = True + conf.options.SDL12 = True + conf.options.NO_VGUI = True + conf.options.GL = False + conf.options.LOW_MEMORY = 1 + conf.options.SINGLE_BINARY = True conf.options.NO_ASYNC_RESOLVE = True conf.define('XASH_SDLMAIN', 1) enforce_pic = False - # useless to change toolchain path, as toolchain meant to be placed in this path - toolchain_path = '/opt/toolchains/motomagx/arm-eabi2/lib/' - conf.env.INCLUDES_MAGX = [toolchain_path + i for i in ['ezx-z6/include', 'qt-2.3.8/include']] - conf.env.LIBPATH_MAGX = [toolchain_path + i for i in ['ezx-z6/lib', 'qt-2.3.8/lib']] - conf.env.LINKFLAGS_MAGX = ['-Wl,-rpath-link=' + i for i in conf.env.LIBPATH_MAGX] - for lib in ['qte-mt', 'ezxappbase', 'ezxpm', 'log_util']: - conf.check_cc(lib=lib, use='MAGX', uselib_store='MAGX') - - if enforce_pic: - # Every static library must have fPIC - if conf.env.DEST_OS != 'win32' and '-fPIC' in conf.env.CFLAGS_cshlib: - conf.env.append_unique('CFLAGS_cstlib', '-fPIC') - conf.env.append_unique('CXXFLAGS_cxxstlib', '-fPIC') - else: - if '-fPIC' in conf.env.CFLAGS_cshlib: - conf.env.CFLAGS_cshlib.remove('-fPIC') - if '-fPIC' in conf.env.CXXFLAGS_cshlib: - conf.env.CXXFLAGS_cxxshlib.remove('-fPIC') - if '-fPIC' in conf.env.CFLAGS_MACBUNDLE: - conf.env.CFLAGS_MACBUNDLE.remove('-fPIC') - if '-fPIC' in conf.env.CXXFLAGS_MACBUNDLE: - conf.env.CXXFLAGS_MACBUNDLE.remove('-fPIC') + if conf.env.STATIC_LINKING: + enforce_pic = False # PIC may break full static builds + + conf.check_pic(enforce_pic) # We restrict 64-bit builds ONLY for Win/Linux/OSX running on Intel architecture # Because compatibility with original GoldSrc @@ -202,64 +150,6 @@ def configure(conf): conf.load('force_32bit') - linker_flags = { - 'common': { - 'msvc': ['/DEBUG'], # always create PDB, doesn't affect result binaries - 'gcc': ['-Wl,--no-undefined'], - 'owcc': ['-Wl,option stack=512k'] - }, - 'sanitize': { - 'clang': ['-fsanitize=undefined', '-fsanitize=address'], - 'gcc': ['-fsanitize=undefined', '-fsanitize=address'], - } - } - - compiler_c_cxx_flags = { - 'common': { - # disable thread-safe local static initialization for C++11 code, as it cause crashes on Windows XP - 'msvc': ['/D_USING_V110_SDK71_', '/Zi', '/FS', '/Zc:threadSafeInit-', '/MT'], - 'clang': ['-g', '-gdwarf-2', '-fvisibility=hidden'], - 'gcc': ['-g', '-fvisibility=hidden'], - 'owcc': ['-fno-short-enum', '-ffloat-store', '-g3'] - }, - 'fast': { - 'msvc': ['/O2', '/Oy'], - 'gcc': { - '3': ['-O3', '-fomit-frame-pointer'], - 'default': ['-Ofast', '-funsafe-math-optimizations', '-funsafe-loop-optimizations', '-fomit-frame-pointer'] - }, - 'clang': ['-Ofast'], - 'default': ['-O3'] - }, - 'fastnative': { - 'msvc': ['/O2', '/Oy'], - 'gcc': ['-Ofast', '-march=native', '-funsafe-math-optimizations', '-funsafe-loop-optimizations', '-fomit-frame-pointer'], - 'clang': ['-Ofast', '-march=native'], - 'default': ['-O3'] - }, - 'release': { - 'msvc': ['/O2'], - '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'], - 'default': ['-O3'] - }, - 'debug': { - 'msvc': ['/O1'], - 'gcc': ['-Og'], - 'owcc': ['-O0', '-fno-omit-frame-pointer', '-funwind-tables', '-fno-omit-leaf-frame-pointer'], - 'default': ['-O1'] - }, - 'sanitize': { - 'msvc': ['/Od', '/RTC1'], - 'gcc': ['-Og', '-fsanitize=undefined', '-fsanitize=address'], - 'clang': ['-O0', '-fsanitize=undefined', '-fsanitize=address'], - 'default': ['-O0'] - }, - 'nooptimize': { - 'msvc': ['/Od'], - 'default': ['-O0'] - } - } - compiler_optional_flags = [ # '-Wall', '-Wextra', '-Wpedantic', '-fdiagnostics-color=always', @@ -296,39 +186,13 @@ def configure(conf): '-fnonconst-initializers' # owcc ] - linkflags = conf.get_flags_by_type(linker_flags, conf.options.BUILD_TYPE, conf.env.COMPILER_CC, conf.env.CC_VERSION[0]) - cflags = conf.get_flags_by_type(compiler_c_cxx_flags, conf.options.BUILD_TYPE, conf.env.COMPILER_CC, conf.env.CC_VERSION[0]) - - # Here we don't differentiate C or C++ flags - if conf.options.LTO: - lto_cflags = { - 'msvc': ['/GL'], - 'gcc': ['-flto'], - 'clang': ['-flto'] - } - - lto_linkflags = { - 'msvc': ['/LTCG'], - 'gcc': ['-flto'], - 'clang': ['-flto'] - } - cflags += conf.get_flags_by_compiler(lto_cflags, conf.env.COMPILER_CC) - linkflags += conf.get_flags_by_compiler(lto_linkflags, conf.env.COMPILER_CC) - - if conf.options.POLLY: - polly_cflags = { - 'gcc': ['-fgraphite-identity'], - 'clang': ['-mllvm', '-polly'] - # msvc sosat :( - } - - cflags += conf.get_flags_by_compiler(polly_cflags, conf.env.COMPILER_CC) + cflags, linkflags = conf.get_optimization_flags() # And here C++ flags starts to be treated separately cxxflags = list(cflags) if conf.env.COMPILER_CC != 'msvc': - conf.check_cc(cflags=cflags, linkflags=linkflags, msg= 'Checking for required C flags') - conf.check_cxx(cxxflags=cflags, linkflags=linkflags, msg= 'Checking for required C++ flags') + conf.check_cc(cflags=cflags, linkflags=linkflags, msg='Checking for required C flags') + conf.check_cxx(cxxflags=cflags, linkflags=linkflags, msg='Checking for required C++ flags') conf.env.append_unique('CFLAGS', cflags) conf.env.append_unique('CXXFLAGS', cxxflags) @@ -403,9 +267,7 @@ def configure(conf): conf.env.LIBDIR = conf.env.BINDIR = conf.env.PREFIX conf.define('XASH_BUILD_COMMIT', conf.env.GIT_VERSION if conf.env.GIT_VERSION else 'notset') - - if conf.options.LOW_MEMORY: - conf.define('XASH_LOW_MEMORY', conf.options.LOW_MEMORY) + conf.define('XASH_LOW_MEMORY', conf.options.LOW_MEMORY) for i in SUBDIRS: if not i.is_enabled(conf):