diff --git a/src/mapleall/maple_driver/defs/default/O0_options_clang.def b/src/mapleall/maple_driver/defs/default/O0_options_clang.def index a8edd35c27eedc345376e3e34eabae6a4e7ac976..fb70797f01067addccfdeb08a9b7a1d6fd843524 100644 --- a/src/mapleall/maple_driver/defs/default/O0_options_clang.def +++ b/src/mapleall/maple_driver/defs/default/O0_options_clang.def @@ -1,2 +1 @@ -{"-emit-ast", "", false}, -{"-o", "", false}, \ No newline at end of file +{"-emit-ast", "", false}, \ No newline at end of file diff --git a/src/mapleall/maple_driver/defs/default/O0_options_cpp2mpl.def b/src/mapleall/maple_driver/defs/default/O0_options_cpp2mpl.def index 311847daa5a050a215b149561e1e00c818f5b03b..04bbc03d702e2d0ceb7b15a16077132a597ff6b5 100644 --- a/src/mapleall/maple_driver/defs/default/O0_options_cpp2mpl.def +++ b/src/mapleall/maple_driver/defs/default/O0_options_cpp2mpl.def @@ -1,2 +1 @@ -{} - +{"--enable-variable-array", "", false}, diff --git a/src/mapleall/maple_driver/include/compiler.h b/src/mapleall/maple_driver/include/compiler.h index 089ba8ea0d845e4a01309c20ec6c10b13c73f7df..e27316d8fbb50110b0677b8bbbf8320a474f933c 100644 --- a/src/mapleall/maple_driver/include/compiler.h +++ b/src/mapleall/maple_driver/include/compiler.h @@ -98,28 +98,20 @@ class Compiler { std::vector MakeOption(const MplOptions &options, const Action &action) const; void AppendDefaultOptions(std::vector &finalOptions, - const std::map &defaultOptions, + const std::vector &defaultOptions, bool isDebug) const; void AppendExtraOptions(std::vector &finalOptions, - std::map defaultOptions, const MplOptions &options, bool isDebug) const; void AppendInputsAsOptions(std::vector &finalOptions, const MplOptions &mplOptions, const Action &action) const; - std::map MakeDefaultOptions(const MplOptions &options, - const Action &action) const; + void ReplaceOrInsertOption(std::vector &finalOptions, + const std::string &key, const std::string &value) const; + std::vector MakeDefaultOptions(const MplOptions &options, + const Action &action) const; int Exe(const MplOptions &mplOptions, const std::vector &options) const; const std::string &GetName() const { return name; } - - void ReplaceOption(std::vector &finalOptions, - const std::string &key, const std::string &value) const { - for (auto &opt : finalOptions) { - if (opt.GetKey() == key) { - opt.SetValue(value); - } - } - } }; class Jbc2MplCompiler : public Compiler { diff --git a/src/mapleall/maple_driver/include/driver_option_common.h b/src/mapleall/maple_driver/include/driver_option_common.h index 5f6f08ee4d36bc66016d1eddf534841c65b31cb9..238312b26344d853b4bcbb750ba7ce308c1792be 100644 --- a/src/mapleall/maple_driver/include/driver_option_common.h +++ b/src/mapleall/maple_driver/include/driver_option_common.h @@ -70,7 +70,9 @@ enum DriverOptionIndex { kLdLib, kLdLibPath, kClangMacro, + kClangUMacro, kClangInclude, + kClangISystem, kMapleOut, kCommonOptionEnd, }; diff --git a/src/mapleall/maple_driver/src/clang_compiler.cpp b/src/mapleall/maple_driver/src/clang_compiler.cpp index 002994d595aaa80f5b309f9287a976c52201dd95..6f44c4e882efbd6a1dad5b96dce2e5f88424c0c0 100644 --- a/src/mapleall/maple_driver/src/clang_compiler.cpp +++ b/src/mapleall/maple_driver/src/clang_compiler.cpp @@ -29,16 +29,56 @@ const std::string &ClangCompiler::GetBinName() const { return kBinNameClang; } +static uint32_t FillSpecialDefaulOpt(std::unique_ptr &opt, + const Action &action) { + uint32_t additionalLen = 1; // for -o option + + /* TODO: Add check for the target architecture and OS environment. + * Currently it supports only aarch64 and linux-gnu- + */ + if (kOperatingSystem == "linux-gnu-" && kMachine == "aarch64-") { + additionalLen += 3; + opt = std::make_unique(additionalLen); + + opt[0].SetKey("-isystem"); + opt[0].SetValue(FileUtils::SafeGetenv(kMapleRoot) + "/tools/gcc-linaro-7.5.0/aarch64-linux-gnu/libc/usr/include"); + + opt[1].SetKey("-isystem"); + opt[1].SetValue(FileUtils::SafeGetenv(kMapleRoot) + "/tools/gcc-linaro-7.5.0/lib/gcc/aarch64-linux-gnu/7.5.0/include"); + + opt[2].SetKey("-target"); + opt[2].SetValue("aarch64"); + } else { + CHECK_FATAL(false, "Only linux-gnu OS and aarch64 target are supported \n"); + } + + /* Set last option as -o option */ + opt[additionalLen-1].SetKey("-o"); + opt[additionalLen-1].SetValue(action.GetFullOutputName() + ".ast"); + + return additionalLen; +} + DefaultOption ClangCompiler::GetDefaultOptions(const MplOptions &options, const Action &action) const { - uint32_t len = sizeof(kClangDefaultOptions) / sizeof(MplOption); - DefaultOption defaultOptions = { std::make_unique(len), len }; + DefaultOption defaultOptions; + uint32_t fullLen = 0; + uint32_t defaultLen = 0; + uint32_t additionalLen = 0; + std::unique_ptr additionalOptions; + + additionalLen = FillSpecialDefaulOpt(additionalOptions, action); + defaultLen = sizeof(kClangDefaultOptions) / sizeof(MplOption); + fullLen = defaultLen + additionalLen; - for (uint32_t i = 0; i < len; ++i) { + defaultOptions = { std::make_unique(fullLen), fullLen }; + + for (uint32_t i = 0; i < defaultLen; ++i) { defaultOptions.mplOptions[i] = kClangDefaultOptions[i]; } - - CHECK_FATAL((len > 1), "Option is hardcoded in O0_options_clang.def file \n"); - defaultOptions.mplOptions[1].SetValue(action.GetFullOutputName() + ".ast"); + for (uint32_t defInd = defaultLen, additionalInd = 0; + additionalInd < additionalLen; ++additionalInd) { + defaultOptions.mplOptions[defInd++] = additionalOptions[additionalInd]; + } for (uint32_t i = 0; i < defaultOptions.length; ++i) { defaultOptions.mplOptions[i].SetValue( diff --git a/src/mapleall/maple_driver/src/compiler.cpp b/src/mapleall/maple_driver/src/compiler.cpp index ea872fbe49a30553f0d35ed47036a4f8001cbc34..8fb9f24cb4aa27387d40d6787cf440e86c25e33e 100644 --- a/src/mapleall/maple_driver/src/compiler.cpp +++ b/src/mapleall/maple_driver/src/compiler.cpp @@ -64,34 +64,33 @@ ErrorCode Compiler::Compile(MplOptions &options, const Action &action, std::vector Compiler::MakeOption(const MplOptions &options, const Action &action) const { std::vector finalOptions; - std::map defaultOptions = MakeDefaultOptions(options, action); + std::vector defaultOptions = MakeDefaultOptions(options, action); AppendInputsAsOptions(finalOptions, options, action); AppendDefaultOptions(finalOptions, defaultOptions, options.HasSetDebugFlag()); - AppendExtraOptions(finalOptions, defaultOptions, options, options.HasSetDebugFlag()); + AppendExtraOptions(finalOptions, options, options.HasSetDebugFlag()); return finalOptions; } void Compiler::AppendDefaultOptions(std::vector &finalOptions, - const std::map &defaultOptions, + const std::vector &defaultOptions, bool isDebug) const { for (const auto &defaultIt : defaultOptions) { - (void)finalOptions.push_back(defaultIt.second); + (void)finalOptions.push_back(defaultIt); } if (isDebug) { LogInfo::MapleLogger() << Compiler::GetName() << " Default Options: "; for (const auto &defaultIt : defaultOptions) { - LogInfo::MapleLogger() << defaultIt.first << " " - << defaultIt.second.GetValue(); + LogInfo::MapleLogger() << defaultIt.GetKey() << " " + << defaultIt.GetValue(); } LogInfo::MapleLogger() << '\n'; } } void Compiler::AppendExtraOptions(std::vector &finalOptions, - std::map defaultOptions, const MplOptions &options, bool isDebug) const { const std::string &binName = GetTool(); auto exeOption = options.GetExeOptions().find(binName); @@ -107,13 +106,18 @@ void Compiler::AppendExtraOptions(std::vector &finalOptions, prefix = "--"; } - const std::string key = prefix + opt.OptionKey(); + const std::string baseKey = opt.OptionKey(); + const std::string key = prefix + baseKey; const std::string value = opt.Args(); - /* Update option if needed */ - auto it = defaultOptions.find(key); - if (it != defaultOptions.end()) { - ReplaceOption(finalOptions, key, value); + /* Default behaviour: extra options do not replace default options, + * because it can be some additional option with the same key. + * For example: we can have some default -isystem SYSTEM pathes option. + * And if some additional -isystem SYSTEM pathes is added, it's not correct + * to replace them (SYSTEM pathes msut be extended (not replaced)). + * If you need to replace some special option, check and replace it here */ + if (baseKey == "o") { + ReplaceOrInsertOption(finalOptions, key, value); } else { finalOptions.push_back(MplOption(key, value)); } @@ -129,6 +133,21 @@ void Compiler::AppendExtraOptions(std::vector &finalOptions, } } +void Compiler::ReplaceOrInsertOption(std::vector &finalOptions, + const std::string &key, const std::string &value) const { + bool wasFound = false; + for (auto &opt : finalOptions) { + if (opt.GetKey() == key) { + opt.SetValue(value); + wasFound = true; + } + } + + if (!wasFound) { + finalOptions.push_back(MplOption(key, value)); + } +} + void Compiler::AppendInputsAsOptions(std::vector &finalOptions, const MplOptions &mplOptions, const Action &action) const { std::vector splittedInputFileNames; @@ -140,14 +159,13 @@ void Compiler::AppendInputsAsOptions(std::vector &finalOptions, } } -std::map Compiler::MakeDefaultOptions(const MplOptions &options, - const Action &action) const { +std::vector Compiler::MakeDefaultOptions(const MplOptions &options, + const Action &action) const { DefaultOption rawDefaultOptions = GetDefaultOptions(options, action); - std::map defaultOptions; + std::vector defaultOptions; if (rawDefaultOptions.mplOptions != nullptr) { for (uint32_t i = 0; i < rawDefaultOptions.length; ++i) { - (void)defaultOptions.insert(std::make_pair(rawDefaultOptions.mplOptions[i].GetKey(), - rawDefaultOptions.mplOptions[i])); + (void)defaultOptions.push_back(rawDefaultOptions.mplOptions[i]); } } return defaultOptions; diff --git a/src/mapleall/maple_driver/src/driver_option_common.cpp b/src/mapleall/maple_driver/src/driver_option_common.cpp index 59ba0e46aa1b1c710af5337d7c48687efeea231e..7d992f057441872677f2f5a78efa2b37694d7222 100644 --- a/src/mapleall/maple_driver/src/driver_option_common.cpp +++ b/src/mapleall/maple_driver/src/driver_option_common.cpp @@ -483,6 +483,15 @@ const mapleOption::Descriptor kUsages[] = { " -D = \tDefine to (or 1 if omitted)\n", "all", {"clang"} }, + { kClangMacro, + 0, + "U", + "", + kBuildTypeAll, + kArgCheckPolicyRequired, + " -U \tUndefine macro \n", + "all", + {"clang"} }, { kClangInclude, 0, "I", @@ -492,6 +501,15 @@ const mapleOption::Descriptor kUsages[] = { " -I \tAdd directory to include search path\n", "all", {"clang"} }, + { kClangISystem, + 0, + "isystem", + "", + kBuildTypeAll, + kArgCheckPolicyRequired, + " -isystem \tAdd directory to SYSTEM include search path\n", + "all", + {"clang"} }, { kMaplePhaseOnly, kEnable, "", diff --git a/testsuite/c_test/driver_test/DRIVER0003-macro/expected.txt b/testsuite/c_test/driver_test/DRIVER0003-macro/expected.txt new file mode 100644 index 0000000000000000000000000000000000000000..f57d321a226e10e8394403ecfbcc7483a1cc7559 --- /dev/null +++ b/testsuite/c_test/driver_test/DRIVER0003-macro/expected.txt @@ -0,0 +1,4 @@ +TEST1 +HELPER TEST1 +RES: 40 +HELPER TEST2 10 diff --git a/testsuite/c_test/driver_test/DRIVER0003-macro/helper.c b/testsuite/c_test/driver_test/DRIVER0003-macro/helper.c new file mode 100644 index 0000000000000000000000000000000000000000..9ad6a5b4e3a812f216c2f72a9881363ace0bfe3d --- /dev/null +++ b/testsuite/c_test/driver_test/DRIVER0003-macro/helper.c @@ -0,0 +1,31 @@ +/* + * Copyright (C) [2021] Futurewei Technologies, Inc. All rights reverved. + * + * Licensed under the Mulan Permissive Software License v2. + * You can use this software according to the terms and conditions of the MulanPSL - 2.0. + * You may obtain a copy of MulanPSL - 2.0 at: + * + * https://opensource.org/licenses/MulanPSL-2.0 + * + * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR + * FIT FOR A PARTICULAR PURPOSE. + * See the MulanPSL - 2.0 for more details. + */ + +#include +#include + +int helper(int in) +{ +#ifdef TEST1 + printf("HELPER TEST1\n"); +#endif + +#ifdef TEST2 + printf("HELPER TEST2\n"); +#endif + + return in * 2; +} + diff --git a/testsuite/c_test/driver_test/DRIVER0003-macro/main.c b/testsuite/c_test/driver_test/DRIVER0003-macro/main.c new file mode 100644 index 0000000000000000000000000000000000000000..c967a6db28d63a92c82220a204fc321fdc827da8 --- /dev/null +++ b/testsuite/c_test/driver_test/DRIVER0003-macro/main.c @@ -0,0 +1,38 @@ +/* + * Copyright (C) [2021] Futurewei Technologies, Inc. All rights reverved. + * + * Licensed under the Mulan Permissive Software License v2. + * You can use this software according to the terms and conditions of the MulanPSL - 2.0. + * You may obtain a copy of MulanPSL - 2.0 at: + * + * https://opensource.org/licenses/MulanPSL-2.0 + * + * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR + * FIT FOR A PARTICULAR PURPOSE. + * See the MulanPSL - 2.0 for more details. + */ + +#include +#include + +int helper(int in); + +int main(void) +{ +#ifdef TEST1 + printf("TEST1\n"); +#endif + +#ifdef TEST2 + printf("TEST2\n"); +#endif + + int res = helper(NUM); + printf("RES: %d\n", res); + +#ifdef TEST3 + printf("HELPER TEST2 %d\n", EXTRANUM); +#endif + return 0; +} diff --git a/testsuite/c_test/driver_test/DRIVER0003-macro/test.cfg b/testsuite/c_test/driver_test/DRIVER0003-macro/test.cfg new file mode 100644 index 0000000000000000000000000000000000000000..ad25f535d3d7ec99f4b8fdd1f687763921a63422 --- /dev/null +++ b/testsuite/c_test/driver_test/DRIVER0003-macro/test.cfg @@ -0,0 +1,2 @@ +compile(APP="main.c helper.c",OPTION="--no-maple-phase --static -DTEST1 -DNUM=20 -DTEST2 -UTEST2 -D TEST3 -D EXTRANUM=10") +run(a) diff --git a/testsuite/c_test/driver_test/DRIVER0004-syslibs/expected.txt b/testsuite/c_test/driver_test/DRIVER0004-syslibs/expected.txt new file mode 100644 index 0000000000000000000000000000000000000000..9ecdb58fc1a576ae1ad044f3b5894eb604e44cdf --- /dev/null +++ b/testsuite/c_test/driver_test/DRIVER0004-syslibs/expected.txt @@ -0,0 +1 @@ +sqrt(1024.0) = 32.0 diff --git a/testsuite/c_test/driver_test/DRIVER0004-syslibs/main.c b/testsuite/c_test/driver_test/DRIVER0004-syslibs/main.c new file mode 100644 index 0000000000000000000000000000000000000000..cb346561e86d715d2b8fac47dc4e0b566b9ad987 --- /dev/null +++ b/testsuite/c_test/driver_test/DRIVER0004-syslibs/main.c @@ -0,0 +1,29 @@ +/* + * Copyright (C) [2021] Futurewei Technologies, Inc. All rights reverved. + * + * Licensed under the Mulan Permissive Software License v2. + * You can use this software according to the terms and conditions of the MulanPSL - 2.0. + * You may obtain a copy of MulanPSL - 2.0 at: + * + * https://opensource.org/licenses/MulanPSL-2.0 + * + * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR + * FIT FOR A PARTICULAR PURPOSE. + * See the MulanPSL - 2.0 for more details. + */ + +#include +#include + +#define PI 3.14 + +int main() +{ + double param, result; + param = 1024.0; + result = sqrt(param); + + printf ("sqrt(%.1f) = %.1f\n", param, result ); + return 0; +} diff --git a/testsuite/c_test/driver_test/DRIVER0004-syslibs/test.cfg b/testsuite/c_test/driver_test/DRIVER0004-syslibs/test.cfg new file mode 100644 index 0000000000000000000000000000000000000000..d576379fe053ad817191ee4f5461fd3429b09225 --- /dev/null +++ b/testsuite/c_test/driver_test/DRIVER0004-syslibs/test.cfg @@ -0,0 +1,2 @@ +compile(APP=main.c,OPTION="--no-maple-phase --static -lm") +run(a)