diff options
author | dec05eba <dec05eba@protonmail.com> | 2017-12-28 16:57:06 +0100 |
---|---|---|
committer | dec05eba <dec05eba@protonmail.com> | 2017-12-28 16:57:11 +0100 |
commit | 1f583ebb6e3973c992d59886659bf53ff87f41de (patch) | |
tree | cfefa98bf921d443f577785d744a5fe6d12f2e86 /src | |
parent | da405ce0c1c9c675aee1710917d9d89b7eb34922 (diff) |
Add optimization level option to building
Diffstat (limited to 'src')
-rw-r--r-- | src/Conf.cpp | 10 | ||||
-rw-r--r-- | src/GlobalLib.cpp | 22 | ||||
-rw-r--r-- | src/main.cpp | 80 |
3 files changed, 95 insertions, 17 deletions
diff --git a/src/Conf.cpp b/src/Conf.cpp index 97d562c..d6aee2c 100644 --- a/src/Conf.cpp +++ b/src/Conf.cpp @@ -332,6 +332,16 @@ namespace sibs return Parser::parse(code, callback); } + const char* asString(OptimizationLevel optLevel) + { + switch(optLevel) + { + case OPT_LEV_NONE: return "none"; + case OPT_LEV_DEBUG: return "debug"; + case OPT_LEV_RELEASE: return "release"; + } + } + void SibsConfig::processObject(StringView name) { currentObject = name; diff --git a/src/GlobalLib.cpp b/src/GlobalLib.cpp index 30a044f..d994a27 100644 --- a/src/GlobalLib.cpp +++ b/src/GlobalLib.cpp @@ -60,7 +60,7 @@ namespace sibs return _tinydir_strncmp(path, subPathOf.c_str(), subPathOf.size()) == 0; } - Result<string> GlobalLib::getLibsLinkerFlags(const string &globalLibRootDir, const string &name, const string &version, LinkerFlagCallbackFunc staticLinkerFlagCallbackFunc, LinkerFlagCallbackFunc dynamicLinkerFlagCallbackFunc) + Result<string> GlobalLib::getLibsLinkerFlags(const SibsConfig &parentConfig, const string &globalLibRootDir, const string &name, const string &version, LinkerFlagCallbackFunc staticLinkerFlagCallbackFunc, LinkerFlagCallbackFunc dynamicLinkerFlagCallbackFunc) { Result<bool> packageExistsResult = validatePackageExists(globalLibRootDir, name); if(packageExistsResult.isErr()) @@ -110,7 +110,7 @@ namespace sibs } } - SibsConfig sibsConfig(packageDir); + SibsConfig sibsConfig(packageDir, parentConfig.getOptimizationLevel()); Result<bool> result = Config::readFromFile(projectConfFilePath.c_str(), sibsConfig); if(result.isErr()) return Result<string>::Err(result.getErrMsg()); @@ -175,8 +175,18 @@ namespace sibs } else { - string debugBuildPath = packageDir + "/sibs-build/debug"; - string libPath = debugBuildPath; + string buildPath = packageDir + "/sibs-build/"; + switch(sibsConfig.getOptimizationLevel()) + { + case OPT_LEV_DEBUG: + buildPath += "debug"; + break; + case OPT_LEV_RELEASE: + buildPath += "release"; + break; + } + + string libPath = buildPath; libPath += "/lib"; libPath += name; if(libraryType == backend::Ninja::LibraryType::STATIC) @@ -199,11 +209,11 @@ namespace sibs // TODO: Use different directories depending on the project type, but .o build files should be in the same directory // no matter what project type, since they are used for executables, static/dynamic libraries - Result<bool> createBuildDirResult = createDirectoryRecursive(debugBuildPath.c_str()); + Result<bool> createBuildDirResult = createDirectoryRecursive(buildPath.c_str()); if(createBuildDirResult.isErr()) return Result<string>::Err(createBuildDirResult); - Result<bool> buildFileResult = ninja.build(sibsConfig, debugBuildPath.c_str(), staticLinkerFlagCallbackFunc, dynamicLinkerFlagCallbackFunc); + Result<bool> buildFileResult = ninja.build(sibsConfig, buildPath.c_str(), staticLinkerFlagCallbackFunc, dynamicLinkerFlagCallbackFunc); if (buildFileResult.isErr()) return Result<string>::Err(buildFileResult.getErrMsg()); diff --git a/src/main.cpp b/src/main.cpp index 79d4d9d..0f20ea0 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -30,6 +30,21 @@ void usage() exit(1); } +void usageBuild() +{ + printf("Usage: sibs build [project_path] [--debug|--release]\n\n"); + printf("Build a sibs project\n\n"); + printf("Options:\n"); + printf(" project_path\t\t The directory containing a project.conf file - Optional (default: current working directory)\n"); + printf(" --debug|--release\t\tOptimization level to build project and dependencies with (if not a system package) - Optional (default: --debug)\n"); + printf("Examples:\n"); + printf(" sibs build\n"); + printf(" sibs build dirA/dirB\n"); + printf(" sibs build --release\n"); + printf(" sibs build dirA --release\n"); + exit(1); +} + void usageNew() { printf("Usage: sibs new <project_name> <--exec|--static|--dynamic>\n\n"); @@ -100,17 +115,50 @@ bool isPathSubPathOf(const string &path, const string &subPathOf) int buildProject(int argc, const char **argv) { - if(argc > 1) + if(argc > 2) + usageBuild(); + + OptimizationLevel optimizationLevel = OPT_LEV_NONE; + string projectPath; + + for(int i = 0; i < argc; ++i) { - cerr << "Expected 'build' command to only be followed by one argument which is the path to a directory that contains a project.conf file, "; - cerr << "or none; in which case build would use the working directory as target directory" << endl << endl; - usage(); + const char *arg = argv[i]; + if(strcmp(arg, "--debug") == 0) + { + if(optimizationLevel != OPT_LEV_NONE) + { + fprintf(stderr, "Error: Optimization level defined more than once. First defined as %s then as %s\n", asString(optimizationLevel), "debug"); + usageBuild(); + } + optimizationLevel = OPT_LEV_DEBUG; + } + else if(strcmp(arg, "--release") == 0) + { + if(optimizationLevel != OPT_LEV_NONE) + { + fprintf(stderr, "Error: Optimization level defined more than once. First defined as %s then as %s\n", asString(optimizationLevel), "debug"); + usageBuild(); + } + optimizationLevel = OPT_LEV_RELEASE; + } + else + { + if(!projectPath.empty()) + { + fprintf(stderr, "Error: Project path was defined more than once. First defined as %s then as %s\n", projectPath.c_str(), arg); + usageBuild(); + } + projectPath = arg; + } } - // TODO: If argc == 0 and working directory does not contain project.conf, then search every parent directory until one is found - string projectPath = "."; - if(argc == 1) - projectPath = argv[0]; + if(optimizationLevel == OPT_LEV_NONE) + optimizationLevel = OPT_LEV_DEBUG; + + // TODO: If projectPath is not defined and working directory does not contain project.conf, then search every parent directory until one is found + if(projectPath.empty()) + projectPath = "."; validateDirectoryPath(projectPath.c_str()); if(projectPath.back() != '/') @@ -128,7 +176,7 @@ int buildProject(int argc, const char **argv) projectConfFilePath += "/project.conf"; validateFilePath(projectConfFilePath.c_str()); - SibsConfig sibsConfig(projectPath); + SibsConfig sibsConfig(projectPath, optimizationLevel); Result<bool> result = Config::readFromFile(projectConfFilePath.c_str(), sibsConfig); if(result.isErr()) { @@ -186,8 +234,18 @@ int buildProject(int argc, const char **argv) }; walkDir(projectPath.c_str(), collectSourceFiles); - string debugBuildPath = projectPath + "/sibs-build/debug"; - Result<bool> buildFileResult = ninja.build(sibsConfig, debugBuildPath.c_str()); + string buildPath = projectPath + "/sibs-build/"; + switch(sibsConfig.getOptimizationLevel()) + { + case OPT_LEV_DEBUG: + buildPath += "debug"; + break; + case OPT_LEV_RELEASE: + buildPath += "release"; + break; + } + + Result<bool> buildFileResult = ninja.build(sibsConfig, buildPath.c_str()); if(buildFileResult.isErr()) { cerr << "Failed to build ninja file: " << buildFileResult.getErrMsg() << endl; |