Folding build_tools back into the main repo for simplicity.

This commit is contained in:
Ben Vanik
2015-12-30 16:52:49 -08:00
parent 214532a3e8
commit 952d35911c
47 changed files with 6903 additions and 82 deletions

162
tools/build/premake Normal file
View File

@@ -0,0 +1,162 @@
#!/usr/bin/env python
# Copyright 2015 Ben Vanik. All Rights Reserved.
"""Premake trampoline script.
"""
__author__ = 'ben.vanik@gmail.com (Ben Vanik)'
import os
import subprocess
import sys
self_path = os.path.dirname(os.path.abspath(__file__))
root_path = os.path.join(self_path, '..', '..')
premake_path = os.path.join(root_path, 'third_party', 'premake-core')
def main():
# First try the freshly-built premake.
premake5_bin = os.path.join(premake_path, 'bin', 'release', 'premake5')
if not has_bin(premake5_bin):
# No fresh build, so fallback to checked in copy (which we may not have).
premake5_bin = os.path.join(self_path, 'bin', 'premake5')
if not has_bin(premake5_bin):
# Still no valid binary, so build it.
print('premake5 executable not found, attempting build...')
build_premake()
premake5_bin = os.path.join(premake_path, 'bin', 'release', 'premake5')
if not has_bin(premake5_bin):
# Nope, boned.
print('ERROR: cannot build premake5 executable.')
sys.exit(1)
# Ensure the submodule has been checked out.
if not os.path.exists(os.path.join(premake_path, 'scripts', 'package.lua')):
print('third_party/premake-core was not present; run xb setup...')
sys.exit(1)
return
return_code = shell_call([
premake5_bin,
'--scripts=%s' % (premake_path),
] + sys.argv[1:],
throw_on_error=False)
sys.exit(return_code)
def build_premake():
"""Builds premake from source.
"""
cwd = os.getcwd()
try:
os.chdir(premake_path)
if sys.platform == 'darwin':
shell_call([
'make',
'-f', 'Bootstrap.mak',
'osx',
])
elif sys.platform == 'win32':
# TODO(benvanik): import VS environment.
shell_call([
'nmake',
'-f', 'Bootstrap.mak',
'windows',
])
else:
shell_call([
'make',
'-f', 'Bootstrap.mak',
'linux',
])
finally:
os.chdir(cwd)
pass
def has_bin(bin):
"""Checks whether the given binary is present.
"""
for path in os.environ["PATH"].split(os.pathsep):
path = path.strip('"')
exe_file = os.path.join(path, bin)
if os.path.isfile(exe_file) and os.access(exe_file, os.X_OK):
return True
exe_file = exe_file + '.exe'
if os.path.isfile(exe_file) and os.access(exe_file, os.X_OK):
return True
return None
def shell_call(command, throw_on_error=True, stdout_path=None):
"""Executes a shell command.
Args:
command: Command to execute, as a list of parameters.
throw_on_error: Whether to throw an error or return the status code.
stdout_path: File path to write stdout output to.
Returns:
If throw_on_error is False the status code of the call will be returned.
"""
stdout_file = None
if stdout_path:
stdout_file = open(stdout_path, 'w')
result = 0
try:
if throw_on_error:
result = 1
subprocess.check_call(command, shell=False, stdout=stdout_file)
result = 0
else:
result = subprocess.call(command, shell=False, stdout=stdout_file)
finally:
if stdout_file:
stdout_file.close()
return result
def git_submodule_update():
"""Runs a full recursive git submodule init and update.
Older versions of git do not support 'update --init --recursive'. We could
check and run it on versions that do support it and speed things up a bit.
"""
if True:
shell_call([
'git',
'submodule',
'update',
'--init',
'--recursive',
])
else:
shell_call([
'git',
'submodule',
'init',
])
shell_call([
'git',
'submodule',
'foreach',
'--recursive',
'git',
'submodule',
'init',
])
shell_call([
'git',
'submodule',
'update',
'--recursive',
])
if __name__ == '__main__':
main()

5
tools/build/premake5.lua Normal file
View File

@@ -0,0 +1,5 @@
include("scripts/build_paths.lua")
include("scripts/force_compile_as_c.lua")
include("scripts/force_compile_as_cc.lua")
include("scripts/platform_files.lua")
include("scripts/test_suite.lua")

View File

@@ -0,0 +1,10 @@
build_root = "build"
build_bin = build_root .. "/bin/%{cfg.platform}/%{cfg.buildcfg}"
build_gen = build_root .. "/gen/%{cfg.platform}/%{cfg.buildcfg}"
build_obj = build_root .. "/obj/%{cfg.platform}/%{cfg.buildcfg}"
build_tools = "tools/build"
build_scripts = build_tools .. "/scripts"
build_tools_src = build_tools .. "/src"
platform_suffix = "win"

View File

@@ -0,0 +1,28 @@
if premake.override then
local forced_c_files = {}
-- Forces all of the given .c and .cc files to be compiled as if they were C.
function force_compile_as_c(files)
for _, val in ipairs(files) do
for _, fname in ipairs(os.matchfiles(val)) do
table.insert(forced_c_files, path.getabsolute(fname))
end
end
end
-- for gmake
premake.override(path, "iscfile", function(base, fname)
if table.contains(forced_c_files, fname) then
return true
else
return base(fname)
end
end)
-- for msvc
premake.override(premake.vstudio.vc2010, "additionalCompileOptions", function(base, cfg, condition)
if cfg.abspath and table.contains(forced_c_files, cfg.abspath) then
_p(3,'<CompileAs %s>CompileAsC</CompileAs>', condition)
end
return base(cfg, condition)
end)
end

View File

@@ -0,0 +1,28 @@
if premake.override then
local forced_cc_files = {}
-- Forces all of the given .c files to be compiled as if they were C++.
function force_compile_as_cc(files)
for _, val in ipairs(files) do
for _, fname in ipairs(os.matchfiles(val)) do
table.insert(forced_cc_files, path.getabsolute(fname))
end
end
end
-- for gmake
premake.override(path, "iscfile", function(base, fname)
if table.contains(forced_cc_files, fname) then
return false
else
return base(fname)
end
end)
-- for msvc
premake.override(premake.vstudio.vc2010, "additionalCompileOptions", function(base, cfg, condition)
if cfg.abspath and table.contains(forced_cc_files, cfg.abspath) then
_p(3,'<CompileAs %s>CompileAsCpp</CompileAs>', condition)
end
return base(cfg, condition)
end)
end

View File

@@ -0,0 +1,41 @@
include("build_paths.lua")
include("util.lua")
local function match_platform_files(base_path, base_match)
files({
base_path.."/"..base_match..".h",
base_path.."/"..base_match..".c",
base_path.."/"..base_match..".cc",
})
removefiles({base_path.."/".."**_main.cc"})
removefiles({base_path.."/".."**_test.cc"})
removefiles({base_path.."/".."**_posix.h", base_path.."/".."**_posix.cc"})
removefiles({base_path.."/".."**_linux.h", base_path.."/".."**_linux.cc"})
removefiles({base_path.."/".."**_mac.h", base_path.."/".."**_mac.cc"})
removefiles({base_path.."/".."**_win.h", base_path.."/".."**_win.cc"})
filter("platforms:Windows")
files({
base_path.."/"..base_match.."_win.h",
base_path.."/"..base_match.."_win.cc",
})
filter("platforms:Linux")
files({
base_path.."/"..base_match.."_posix.h",
base_path.."/"..base_match.."_posix.cc",
base_path.."/"..base_match.."_linux.h",
base_path.."/"..base_match.."_linux.cc",
})
filter({})
end
-- Adds all .h and .cc files in the current path that match the current platform
-- suffix (_win, etc).
function local_platform_files(base_path)
match_platform_files(base_path or ".", "*")
end
-- Adds all .h and .cc files in the current path and all subpaths that match
-- the current platform suffix (_win, etc).
function recursive_platform_files(base_path)
match_platform_files(base_path or ".", "**")
end

View File

@@ -0,0 +1,75 @@
include("build_paths.lua")
include("util.lua")
newoption({
trigger = "test-suite-mode",
description = "Whether to merge all tests in a test_suite into a single project",
value = "MODE",
allowed = {
{ "individual", "One binary per test." },
{ "combined", "One binary per test suite (default)." },
},
})
local function combined_test_suite(test_suite_name, project_root, base_path, config)
group("tests")
project(test_suite_name)
kind("ConsoleApp")
language("C++")
includedirs(merge_arrays(config["includedirs"], {
project_root.."/"..build_tools,
project_root.."/"..build_tools_src,
project_root.."/"..build_tools.."/third_party/catch/include",
}))
libdirs(merge_arrays(config["libdirs"], {
project_root.."/"..build_bin,
}))
links(merge_arrays(config["links"], {
"gflags",
}))
files({
project_root.."/"..build_tools_src.."/test_suite_main.cc",
base_path.."/**_test.cc",
})
end
local function split_test_suite(test_suite_name, project_root, base_path, config)
local test_paths = os.matchfiles(base_path.."/**_test.cc")
for _, file_path in pairs(test_paths) do
local test_name = file_path:match("(.*).cc")
group("tests/"..test_suite_name)
project(test_suite_name.."-"..test_name)
kind("ConsoleApp")
language("C++")
includedirs(merge_arrays(config["includedirs"], {
project_root.."/"..build_tools,
project_root.."/"..build_tools_src,
project_root.."/"..build_tools.."/third_party/catch/include",
}))
libdirs(merge_arrays(config["libdirs"], {
project_root.."/"..build_bin,
}))
links(merge_arrays(config["links"], {
"gflags",
}))
files({
project_root.."/"..build_tools_src.."/test_suite_main.cc",
file_path,
})
end
end
-- Defines a test suite binary.
-- Can either be a single binary with all tests or one binary per test based on
-- the --test-suite-mode= option.
function test_suite(
test_suite_name, -- Project or group name for the entire suite.
project_root, -- Project root path (with build_tools/ under it).
base_path, -- Base source path to search for _test.cc files.
config) -- Include/lib directories and links for binaries.
if _OPTIONS["test-suite-mode"] == "individual" then
split_test_suite(test_suite_name, project_root, base_path, config)
else
combined_test_suite(test_suite_name, project_root, base_path, config)
end
end

View File

@@ -0,0 +1,50 @@
-- Prints a table and all of its contents.
function print_r(t)
local print_r_cache={}
local function sub_print_r(t, indent)
if (print_r_cache[tostring(t)]) then
print(indent.."*"..tostring(t))
else
print_r_cache[tostring(t)]=true
if (type(t)=="table") then
for pos,val in pairs(t) do
if (type(val)=="table") then
print(indent.."["..pos.."] => "..tostring(t).." {")
sub_print_r(val,indent..string.rep(" ",string.len(pos)+8))
print(indent..string.rep(" ",string.len(pos)+6).."}")
elseif (type(val)=="string") then
print(indent.."["..pos..'] => "'..val..'"')
else
print(indent.."["..pos.."] => "..tostring(val))
end
end
else
print(indent..tostring(t))
end
end
end
if (type(t)=="table") then
print(tostring(t).." {")
sub_print_r(t," ")
print("}")
else
sub_print_r(t," ")
end
print()
end
-- Merges two tables and returns the resulting table.
function merge_tables(t1, t2)
local result = {}
for k,v in pairs(t1 or {}) do result[k] = v end
for k,v in pairs(t2 or {}) do result[k] = v end
return result
end
-- Merges to arrays and returns the resulting array.
function merge_arrays(t1, t2)
local result = {}
for k,v in pairs(t1 or {}) do result[#result + 1] = v end
for k,v in pairs(t2 or {}) do result[#result + 1] = v end
return result
end

View File

@@ -0,0 +1,53 @@
/**
******************************************************************************
* Xenia : Xbox 360 Emulator Research Project *
******************************************************************************
* Copyright 2015 Ben Vanik. All rights reserved. *
* Released under the BSD license - see LICENSE in the root for more details. *
******************************************************************************
*/
#include <gflags/gflags.h>
#include <codecvt>
#include <cstring>
#include <locale>
#include <string>
#include <vector>
#define CATCH_CONFIG_RUNNER
#include "third_party/catch/include/catch.hpp"
namespace xe {
bool has_console_attached() { return true; }
} // namespace xe
// Used in console mode apps; automatically picked based on subsystem.
int main(int argc, wchar_t* argv[]) {
google::SetUsageMessage(std::string("usage: ..."));
google::SetVersionString("1.0");
// Convert all args to narrow, as gflags doesn't support wchar.
int argca = argc;
char** argva = (char**)alloca(sizeof(char*) * argca);
for (int n = 0; n < argca; n++) {
size_t len = wcslen(argv[n]);
argva[n] = (char*)alloca(len + 1);
wcstombs_s(nullptr, argva[n], len + 1, argv[n], _TRUNCATE);
}
// Parse flags; this may delete some of them.
google::ParseCommandLineFlags(&argc, &argva, true);
#if _WIN32
// Setup COM on the main thread.
// NOTE: this may fail if COM has already been initialized - that's OK.
CoInitializeEx(nullptr, COINIT_MULTITHREADED);
#endif // _WIN32
// Run Catch.
int result = Catch::Session().run(argc, argva);
google::ShutDownCommandLineFlags();
return result;
}