Merge pull request #847 from Pentarctagon/Pentarctagon-optimize-build

Add build which uses higher level of optimization
This commit is contained in:
Sergey Popov 2016-11-26 01:18:22 +03:00 committed by GitHub
commit dd1351dc14
4 changed files with 101 additions and 6 deletions

View file

@ -97,6 +97,38 @@ if(NOT CMAKE_BUILD_TYPE)
set( CMAKE_BUILD_TYPE Release CACHE STRING "Choose the type of build, options are: None Debug Release RelWithDebInfo MinSizeRel." FORCE)
endif(NOT CMAKE_BUILD_TYPE)
# Add additional build with optional LTO flags
MESSAGE("Adding Optimize build")
if("${ENABLE_LTO}" STREQUAL "yes")
MESSAGE("LTO is enabled for Optimize build")
set(CMAKE_CXX_FLAGS_OPTIMIZE "-O3 -march=native -flto -s" CACHE STRING "Flags for a more highly optimized build" FORCE)
set(CMAKE_C_FLAGS_OPTIMIZE "-O3 -march=native -flto -s" CACHE STRING "Flags for a more highly optimized build with LTO" FORCE)
set(CMAKE_EXE_LINKER_FLAGS_OPTIMIZE "-fuse-ld=gold" CACHE STRING "Flags for a more highly optimized build with LTO" FORCE)
else()
MESSAGE("LTO is not enabled for Optimize build")
set(CMAKE_CXX_FLAGS_OPTIMIZE "-O3 -march=native -s" CACHE STRING "Flags for a more highly optimized build without LTO" FORCE)
set(CMAKE_C_FLAGS_OPTIMIZE "-O3 -march=native -s" CACHE STRING "Flags for a more highly optimized build without LTO" FORCE)
set(CMAKE_EXE_LINKER_FLAGS_OPTIMIZE "" CACHE STRING "" FORCE)
endif()
set(CMAKE_SHARED_LINKER_FLAGS_OPTIMIZE "" CACHE STRING "Not used" FORCE)
MARK_AS_ADVANCED(CMAKE_CXX_FLAGS_OPTIMIZE CMAKE_C_FLAGS_OPTIMIZE CMAKE_EXE_LINKER_FLAGS_OPTIMIZE CMAKE_SHARED_LINKER_FLAGS_OPTIMIZE)
# set CMAKE_AR and CMAKE_RANLIB to use LTO-enabled variants if the build is Optimize and LTO is enabled
if("${CMAKE_BUILD_TYPE}" STREQUAL "Optimize" AND "${ENABLE_LTO}" STREQUAL "yes")
MESSAGE("Using gcc-ar and gcc-ranlib")
find_program(LTO_AR NAMES gcc-ar)
find_program(LTO_RANLIB NAMES gcc-ranlib)
set(CMAKE_AR "${LTO_AR}" CACHE STRING "Supports LTO" FORCE)
set(CMAKE_RANLIB "${LTO_RANLIB}" CACHE STRING "Supports LTO" FORCE)
else()
MESSAGE("Using ar and ranlib")
find_program(NON_LTO_AR NAMES ar)
find_program(NON_LTO_RANLIB NAMES ranlib)
set(CMAKE_AR "${NON_LTO_AR}" CACHE STRING "Does not support LTO" FORCE)
set(CMAKE_RANLIB "${NON_LTO_RANLIB}" CACHE STRING "Does not support LTO" FORCE)
endif()
MARK_AS_ADVANCED(LTO_AR LTO_RANLIB NON_LTO_AR NON_LTO_RANLIB)
if(NOT DEFINED ENABLE_DISPLAY_REVISION)
# Test whether the code is used in a repository if not autorevision will
# fail and should be disabled by default. If inside a repository enable
@ -506,6 +538,44 @@ list(REMOVE_ITEM CMAKE_C_FLAGS_MINSIZEREL "-DNDEBUG")
string(REPLACE ";" " " CMAKE_C_FLAGS_MINSIZEREL "${CMAKE_C_FLAGS_MINSIZEREL}")
set(CMAKE_C_FLAGS_MINSIZEREL "${CMAKE_C_FLAGS_MINSIZEREL}" CACHE STRING "removed NDEBUG flag" FORCE)
# add pentiumpro arch for windows builds with -O3, otherwise resulting executable may not work
if(WIN32)
separate_arguments(CMAKE_CXX_FLAGS_RELEASE)
list(APPEND CMAKE_CXX_FLAGS_RELEASE "-march=pentiumpro")
list(REMOVE_DUPLICATES CMAKE_CXX_FLAGS_RELEASE)
string(REPLACE ";" " " CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE}")
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE}" CACHE STRING "added -march=pentiumpro" FORCE)
MESSAGE("added -march=pentiumpro to Release build")
separate_arguments(CMAKE_C_FLAGS_RELEASE)
list(APPEND CMAKE_C_FLAGS_RELEASE "-march=pentiumpro")
list(REMOVE_DUPLICATES CMAKE_C_FLAGS_RELEASE)
string(REPLACE ";" " " CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE}")
set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE}" CACHE STRING "added -march=pentiumpro" FORCE)
MESSAGE("added -march=pentiumpro to Release build")
separate_arguments(CMAKE_CXX_FLAGS_OPTIMIZE)
list(REMOVE_ITEM CMAKE_CXX_FLAGS_OPTIMIZE "-march=native")
list(APPEND CMAKE_CXX_FLAGS_OPTIMIZE "-march=pentiumpro")
list(REMOVE_DUPLICATES CMAKE_CXX_FLAGS_OPTIMIZE)
string(REPLACE ";" " " CMAKE_CXX_FLAGS_OPTIMIZE "${CMAKE_CXX_FLAGS_OPTIMIZE}")
set(CMAKE_CXX_FLAGS_OPTIMIZE "${CMAKE_CXX_FLAGS_OPTIMIZE}" CACHE STRING "added -march=pentiumpro" FORCE)
MESSAGE("added -march=pentiumpro to Optimize build")
separate_arguments(CMAKE_C_FLAGS_OPTIMIZE)
list(REMOVE_ITEM CMAKE_C_FLAGS_OPTIMIZE "-march=native")
list(APPEND CMAKE_C_FLAGS_OPTIMIZE "-march=pentiumpro")
list(REMOVE_DUPLICATES CMAKE_C_FLAGS_OPTIMIZE)
string(REPLACE ";" " " CMAKE_C_FLAGS_OPTIMIZE "${CMAKE_C_FLAGS_OPTIMIZE}")
set(CMAKE_C_FLAGS_OPTIMIZE "${CMAKE_C_FLAGS_OPTIMIZE}" CACHE STRING "added -march=pentiumpro" FORCE)
MESSAGE("added -march=pentiumpro to Optimize build")
endif(WIN32)
# replace the default Debug flag of -g with -O0 -DDEBUG -ggdb3
# this matches the flags of scons' debug build
MESSAGE("Replacing flags used for Debug build")
set(CMAKE_CXX_FLAGS_DEBUG "-O0 -DDEBUG -ggdb3" CACHE STRING "change cmake's Debug flags to match scons' flags" FORCE)
set(CMAKE_C_FLAGS_DEBUG "-O0 -DDEBUG -ggdb3" CACHE STRING "change cmake's Debug flags to match scons' flags" FORCE)
# When the path starts with a / on a Unix system it's an absolute path.
# This means that on Windows the path used is always relative.

View file

@ -47,13 +47,15 @@ def OptionalPath(key, val, env):
opts.AddVariables(
ListVariable('default_targets', 'Targets that will be built if no target is specified in command line.',
"wesnoth,wesnothd", Split("wesnoth wesnothd campaignd cutter exploder test")),
EnumVariable('build', 'Build variant: debug, release profile or base (no subdirectory)', "release", ["release", "debug", "glibcxx_debug", "profile","base"]),
EnumVariable('build', 'Build variant: debug, release profile or base (no subdirectory)', "release", ["optimize", "release", "debug", "glibcxx_debug", "profile", "base"]),
PathVariable('build_dir', 'Build all intermediate files(objects, test programs, etc) under this dir', "build", PathVariable.PathAccept),
('extra_flags_config', "Extra compiler and linker flags to use for configuration and all builds. Whether they're compiler or linker is determined by env.ParseFlags. Unknown flags are compile flags by default. This applies to all extra_flags_* variables", ""),
('extra_flags_base', 'Extra compiler and linker flags to use for release builds', ""),
('extra_flags_release', 'Extra compiler and linker flags to use for release builds', ""),
('extra_flags_debug', 'Extra compiler and linker flags to use for debug builds', ""),
('extra_flags_profile', 'Extra compiler and linker flags to use for profile builds', ""),
('extra_flags_optimize', 'Extra compiler and linker flags to use for optimized builds', ""),
BoolVariable('enable_lto', 'Whether to enable Link Time Optimization', False),
PathVariable('bindir', 'Where to install binaries', "bin", PathVariable.PathAccept),
('cachedir', 'Directory that contains a cache of derived files.', ''),
PathVariable('datadir', 'read-only architecture-independent game data', "$datarootdir/$datadirname", PathVariable.PathAccept),
@ -467,9 +469,23 @@ for env in [test_env, client_env, env]:
env.AppendUnique(CXXFLAGS = Split("-Wold-style-cast"))
if env['sanitize']:
env.AppendUnique(CCFLAGS = ["-fsanitize=" + env["sanitize"]], LINKFLAGS = ["-fsanitize=" + env["sanitize"]])
env["OPT_FLAGS"] = "-O2"
env["OPT_FLAGS"] = "-O3"
env["DEBUG_FLAGS"] = Split("-O0 -DDEBUG -ggdb3")
# because apparently telling the compiler, linker, AND windres to be 32-bit just isn't enough
if env["PLATFORM"] == 'win32':
env["ARCH"] = "-march=pentiumpro"
env["OPT_FLAGS"] = env["OPT_FLAGS"] + " " + env["ARCH"]
else:
env["ARCH"] = "-march=native"
if env["enable_lto"] == True:
env["HIGH_OPT_COMP_FLAGS"] = "-O3 " + env["ARCH"] + " -flto -s"
env["HIGH_OPT_LINK_FLAGS"] = env["HIGH_OPT_COMP_FLAGS"] + " -fuse-ld=gold"
else:
env["HIGH_OPT_COMP_FLAGS"] = "-O3 " + env["ARCH"] + " -s"
env["HIGH_OPT_LINK_FLAGS"] = ""
if "clang" in env["CXX"]:
# Silence warnings about unused -I options and unknown warning switches.
@ -512,11 +528,12 @@ SConscript(dirs = Split("po doc packaging/windows packaging/systemd"))
binaries = Split("wesnoth wesnothd cutter exploder campaignd test")
builds = {
"base" : dict(CCFLAGS = "$OPT_FLAGS"), # Don't build in subdirectory
"base" : dict(CCFLAGS = Split("$OPT_FLAGS")), # Don't build in subdirectory
"debug" : dict(CCFLAGS = Split("$DEBUG_FLAGS")),
"glibcxx_debug" : dict(CPPDEFINES = Split("_GLIBCXX_DEBUG _GLIBCXX_DEBUG_PEDANTIC")),
"release" : dict(CCFLAGS = "$OPT_FLAGS"),
"profile" : dict(CCFLAGS = "-pg", LINKFLAGS = "-pg")
"release" : dict(CCFLAGS = Split("$OPT_FLAGS")),
"profile" : dict(CCFLAGS = "-pg", LINKFLAGS = "-pg"),
"optimize" : dict(CCFLAGS = Split("$HIGH_OPT_COMP_FLAGS"), LINKFLAGS=Split("$HIGH_OPT_LINK_FLAGS"))
}
builds["glibcxx_debug"].update(builds["debug"])
build = env["build"]

View file

@ -609,6 +609,10 @@ def error_action(target, source, env):
raise UserError, "Target disabled because its prerequisites are not met"
def WesnothProgram(env, target, source, can_build, **kw):
if env["build"] == "optimize" and env["enable_lto"] == True:
env["AR"] = 'gcc-ar'
env["RANLIB"] = 'gcc-ranlib'
if can_build:
if env["build"] == "base":
bin = env.Program(target, source, **kw)

View file

@ -42,6 +42,10 @@ env_lua = env.Clone(
CPPPATH = ["$CPPPATH", Dir(".").srcnode()],
CPPDEFINES = ["$CPPDEFINES", env["PLATFORM"] != "win32" and "LUA_USE_POSIX" or []])
if env_lua["build"] == "optimize" and env_lua["enable_lto"] == True:
env_lua["AR"] = 'gcc-ar'
env_lua["RANLIB"] = 'gcc-ranlib'
#if env['strict']:
# env.AppendUnique(CCFLAGS = Split("-Wno-old-style-cast -Wno-useless-cast"))