diff options
| author | Bent Bisballe Nyeng <deva@aasimon.org> | 2021-06-21 21:39:46 +0200 | 
|---|---|---|
| committer | Bent Bisballe Nyeng <deva@aasimon.org> | 2021-06-21 21:39:46 +0200 | 
| commit | b25810b9668abe8f7cc7db24326a98c1b017966e (patch) | |
| tree | 40cda88ffbf8dccfc2d2335783f608966874c58e | |
| parent | c53e622b648635539e4870fd0c9159c5d8c3be4a (diff) | |
Distinguish between host and target builds.
| -rw-r--r-- | libcppbuild.cc | 140 | ||||
| -rw-r--r-- | libcppbuild.h | 16 | ||||
| -rw-r--r-- | task.cc | 37 | ||||
| -rw-r--r-- | task.h | 7 | ||||
| -rw-r--r-- | task_ar.cc | 16 | ||||
| -rw-r--r-- | task_cc.cc | 29 | ||||
| -rw-r--r-- | task_cc.h | 1 | ||||
| -rw-r--r-- | task_ld.cc | 11 | ||||
| -rw-r--r-- | task_so.cc | 19 | 
9 files changed, 195 insertions, 81 deletions
diff --git a/libcppbuild.cc b/libcppbuild.cc index 1fa1bc3..5cd4dc9 100644 --- a/libcppbuild.cc +++ b/libcppbuild.cc @@ -230,7 +230,7 @@ void recompileCheck(const Settings& settings, int argc, char* argv[])  	if(dirty)  	{  		std::cout << "Rebuilding config\n"; -		auto tool = getConfiguration("host-cpp", "/usr/bin/g++"); +		auto tool = getConfiguration(cfg::host_cpp, "/usr/bin/g++");  		auto ret = execute(tool, args, settings.verbose > 0);  		if(ret != 0)  		{ @@ -276,8 +276,74 @@ std::list<std::shared_ptr<Task>> getTasks(const Settings& settings)  	return tasks;  } -int configure(int argc, char* argv[],const Settings& settings) +/* +int configure(int argc, char* argv[])  { +	Settings settings; + +	settings.builddir = "build"; + +	std::string cmd_str; +	for(int i = 0; i < argc; ++i) +	{ +		if(i > 0) +		{ +			cmd_str += " "; +		} +		cmd_str += argv[i]; +	} + +	dg::Options opt; +	int key{256}; + +	std::string build_arch; +	std::string host_arch; + +	opt.add("build-dir", required_argument, 'b', +	        "Set output directory for build files (default: '" + +	        settings.builddir + "').", +	        [&]() { +		        settings.builddir = optarg; +		        return 0; +	        }); + +	opt.add("verbose", no_argument, 'v', +	        "Be verbose. Add multiple times for more verbosity.", +	        [&]() { +		        settings.verbose++; +		        return 0; +	        }); + +	opt.add("build", required_argument, key++, +	        "Configure for building on specified architecture.", +	        [&]() { +		        build_arch = optarg; +		        return 0; +	        }); + +	opt.add("host", required_argument, key++, +	        "Cross-compile to build programs to run on specified architecture.", +	        [&]() { +		        host_arch = optarg; +		        return 0; +	        }); + +	opt.add("help", no_argument, 'h', +	        "Print this help text.", +	        [&]() { +		        std::cout << "configure usage stuff\n"; +		        opt.help(); +		        exit(0); +		        return 0; +	        }); + +	opt.process(argc, argv); + +	if(host_arch.empty()) +	{ +		host_arch = build_arch; +	} +  	auto tasks = getTasks(settings);  	bool needs_cpp{false}; @@ -285,7 +351,7 @@ int configure(int argc, char* argv[],const Settings& settings)  	bool needs_ar{false};  	for(const auto& task :tasks)  	{ -		switch(task->language()) +		switch(task->sourceLanguage())  		{  		case Language::Auto:  			std::cerr << "TargetLanguage not deduced!\n"; @@ -299,46 +365,53 @@ int configure(int argc, char* argv[],const Settings& settings)  			break;  		}  	} -	std::ofstream istr(configurationFile); -	istr << "#include \"libcppbuild.h\"\n\n"; -	istr << "const std::map<std::string, std::string>& configuration()\n"; -	istr << "{\n"; -	istr << "	static std::map<std::string, std::string> c =\n"; -	istr << "	{\n"; -	istr << "		{ \"builddir\", \"build\" },\n"; -	istr << "		{ \"host-cc\", \"/usr/bin/gcc\" },\n"; -	istr << "		{ \"host-cpp\", \"/usr/bin/g++\" },\n"; -	istr << "		{ \"host-ar\", \"/usr/bin/ar\" },\n"; -	istr << "		{ \"host-ld\", \"/usr/bin/ld\" },\n"; -	istr << "		{ \"target-cc\", \"/usr/bin/gcc\" },\n"; -	istr << "		{ \"target-cpp\", \"/usr/bin/g++\" },\n"; -	istr << "		{ \"target-ar\", \"/usr/bin/ar\" },\n"; -	istr << "		{ \"target-ld\", \"/usr/bin/ld\" },\n"; -	istr << "	};\n"; -	istr << "	return c;\n"; -	istr << "}\n"; + +	{ +		std::ofstream istr(configurationFile); +		istr << "#include \"libcppbuild.h\"\n\n"; +		istr << "const std::map<std::string, std::string>& configuration()\n"; +		istr << "{\n"; +		istr << "	static std::map<std::string, std::string> c =\n"; +		istr << "	{\n"; +		istr << "		{ \"cmd\", \"" << cmd_str << "\" },\n"; +		istr << "		{ \"" << cfg::builddir << "\", \"" << settings.builddir << "\" },\n"; +		istr << "		{ \"" << cfg::target_cc << "\", \"/usr/bin/gcc\" },\n"; +		istr << "		{ \"" << cfg::target_cpp << "\", \"/usr/bin/g++\" },\n"; +		istr << "		{ \"" << cfg::target_ar << "\", \"/usr/bin/ar\" },\n"; +		istr << "		{ \"" << cfg::target_ld << "\", \"/usr/bin/ld\" },\n"; +		istr << "		{ \"" << cfg::host_cc << "\", \"/usr/bin/gcc\" },\n"; +		istr << "		{ \"" << cfg::host_cpp << "\", \"/usr/bin/g++\" },\n"; +		istr << "		{ \"" << cfg::host_ar << "\", \"/usr/bin/ar\" },\n"; +		istr << "		{ \"" << cfg::host_ld << "\", \"/usr/bin/ld\" },\n"; +		istr << "	};\n"; +		istr << "	return c;\n"; +		istr << "}\n"; +	} +  	return 0;  }  int main(int argc, char* argv[])  { +	if(argc > 1 && std::string(argv[1]) == "configure") +	{ +		return configure(argc, argv); +	} +  	Settings settings{}; -	settings.builddir = getConfiguration("builddir", "build"); +	settings.builddir = getConfiguration(cfg::builddir, "build");  	settings.parallel_processes =  		std::max(1u, std::thread::hardware_concurrency() * 2 - 1);  	settings.verbose = 0;  	bool write_compilation_database{false};  	std::string compilation_database; - -	if(argc > 1 && std::string(argv[1]) == "configure") -	{ -		return configure(argc, argv, settings); -	} +	bool print_configure_cmd{false};  	dg::Options opt; +	int key{256};  	opt.add("jobs", required_argument, 'j',  	        "Number of parallel jobs. (default: cpucount * 2 - 1 )", @@ -370,6 +443,13 @@ int main(int argc, char* argv[])  		        return 0;  	        }); +	opt.add("configure-cmd", no_argument, key++, +	        "Print commandline for last configure.", +	        [&]() { +		        print_configure_cmd = true; +		        return 0; +	        }); +  	opt.add("database", required_argument, 'd',  	        "Write compilation database json file.",  	        [&]() { @@ -421,6 +501,12 @@ int main(int argc, char* argv[])  		istr << "\n]\n";  	} +	if(print_configure_cmd) +	{ +		std::cout << getConfiguration("cmd") << "\n"; +		return 0; +	} +  	for(auto task : tasks)  	{  		if(task->registerDepTasks(tasks)) diff --git a/libcppbuild.h b/libcppbuild.h index 81e7a9e..9ea3fb1 100644 --- a/libcppbuild.h +++ b/libcppbuild.h @@ -54,6 +54,22 @@ int reg(const char* location, BuildConfigurations (*cb)());  #define UNIQUE_NAME(base) CONCAT(base, __LINE__)  #define REG(cb) namespace { int UNIQUE_NAME(unique) = reg(__FILE__, cb); } +// Predefined configuration keys +namespace cfg +{ +constexpr auto builddir = "builddir"; + +constexpr auto target_cc = "target-cc"; +constexpr auto target_cpp = "target-cpp"; +constexpr auto target_ar = "target-ar"; +constexpr auto target_ld = "target-ld"; + +constexpr auto host_cc = "host-cc"; +constexpr auto host_cpp = "host-cpp"; +constexpr auto host_ar = "host-ar"; +constexpr auto host_ld = "host-ld"; +} +  const std::map<std::string, std::string>& configuration();  bool hasConfiguration(const std::string& key);  const std::string& getConfiguration(const std::string& key, @@ -7,6 +7,7 @@ Task::Task(const BuildConfiguration& config,             const std::vector<std::string>& depends)  	: dependsStr(depends)  	, config(config) +	, output_system(config.system)  {  } @@ -97,7 +98,39 @@ TargetType Task::targetType() const  	return target_type;  } -Language Task::language() const +Language Task::sourceLanguage() const  { -	return _language; +	return source_language; +} + +OutputSystem Task::outputSystem() const +{ +	return output_system; +} + +std::string Task::compiler() const +{ +	switch(sourceLanguage()) +	{ +	case Language::C: +		switch(outputSystem()) +		{ +		case OutputSystem::Target: +			return getConfiguration(cfg::target_cc, "/usr/bin/gcc"); +		case OutputSystem::BuildHost: +			return getConfiguration(cfg::host_cc, "/usr/bin/gcc"); +		} +	case Language::Cpp: +		switch(outputSystem()) +		{ +		case OutputSystem::Target: +			return getConfiguration(cfg::target_cpp, "/usr/bin/g++"); +		case OutputSystem::BuildHost: +			return getConfiguration(cfg::host_cpp, "/usr/bin/g++"); +		} +	default: +		std::cerr << "Unknown CC target type\n"; +		exit(1); +		break; +	}  } @@ -39,7 +39,9 @@ public:  	const BuildConfiguration& buildConfig() const;  	TargetType targetType() const; -	Language language() const; +	Language sourceLanguage() const; +	OutputSystem outputSystem() const; +	std::string compiler() const;  protected:  	std::atomic<State> task_state{State::Unknown}; @@ -50,5 +52,6 @@ protected:  	std::list<std::shared_ptr<Task>> dependsTasks;  	const BuildConfiguration& config;  	TargetType target_type{TargetType::Auto}; -	Language _language{Language::Auto}; +	Language source_language{Language::Auto}; +	OutputSystem output_system{OutputSystem::Target};  }; @@ -65,13 +65,13 @@ TaskAR::TaskAR(const BuildConfiguration& config,  	flagsFile += ".flags";  	target_type = TargetType::StaticLibrary; -	_language = Language::C; +	source_language = Language::C;  	for(const auto& source : config.sources)  	{  		std::filesystem::path sourceFile(source);  		if(sourceFile.extension().string() != ".c")  		{ -			_language = Language::Cpp; +			source_language = Language::Cpp;  		}  	}  } @@ -143,7 +143,17 @@ int TaskAR::runInner()  		std::cout << "AR => " << targetFile.string() << "\n";  	} -	auto tool = getConfiguration("host-ar", "/usr/bin/ar"); +	std::string tool; +	switch(outputSystem()) +	{ +	case OutputSystem::Target: +		tool = getConfiguration(cfg::target_ar, "/usr/bin/ar"); +		break; +	case OutputSystem::BuildHost: +		tool = getConfiguration(cfg::host_ar, "/usr/bin/ar"); +		break; +	} +  	return execute(tool, args, settings.verbose > 0);  } @@ -105,11 +105,11 @@ TaskCC::TaskCC(const BuildConfiguration& config, const Settings& settings,  	target_type = TargetType::Object;  	if(sourceFile.extension().string() == ".c")  	{ -		_language = Language::C; +		source_language = Language::C;  	}  	else  	{ -		_language = Language::Cpp; +		source_language = Language::Cpp;  	}  } @@ -255,7 +255,7 @@ std::string TaskCC::toJSON() const  std::vector<std::string> TaskCC::flags() const  { -	switch(language()) +	switch(sourceLanguage())  	{  	case Language::C:  		return config.cflags; @@ -270,33 +270,14 @@ std::vector<std::string> TaskCC::flags() const  std::string TaskCC::flagsString() const  { -	std::string flagsStr; +	std::string flagsStr = compiler();  	for(const auto& flag : flags())  	{ -		if(!flagsStr.empty()) -		{ -			flagsStr += " "; -		} -		flagsStr += flag; +		flagsStr += " " + flag;  	}  	return flagsStr;  } -std::string TaskCC::compiler() const -{ -	switch(language()) -	{ -	case Language::C: -		return getConfiguration("host-cc"); -	case Language::Cpp: -		return getConfiguration("host-cpp"); -	default: -		std::cerr << "Unknown CC target type\n"; -		exit(1); -		break; -	} -} -  std::vector<std::string> TaskCC::getCompilerArgs() const  {  	auto compiler_flags = flags(); @@ -33,7 +33,6 @@ public:  private:  	std::vector<std::string> flags() const;  	std::string flagsString() const; -	std::string compiler() const;  	std::vector<std::string> getCompilerArgs() const;  	std::filesystem::path sourceFile; @@ -66,13 +66,13 @@ TaskLD::TaskLD(const BuildConfiguration& config,  	flagsFile += ".flags";  	target_type = TargetType::Executable; -	_language = Language::C; +	source_language = Language::C;  	for(const auto& source : config.sources)  	{  		std::filesystem::path sourceFile(source);  		if(sourceFile.extension().string() != ".c")  		{ -			_language = Language::Cpp; +			source_language = Language::Cpp;  		}  	}  } @@ -159,12 +159,7 @@ int TaskLD::runInner()  		std::cout << "LD => " << targetFile.string() << "\n";  	} -	auto tool = getConfiguration("host-cpp", "/usr/bin/g++"); -	if(language() == Language::C) -	{ -		tool = getConfiguration("host-cc", "/usr/bin/gcc"); -	} - +	auto tool = compiler();  	return execute(tool, args, settings.verbose > 0);  } @@ -64,13 +64,13 @@ TaskSO::TaskSO(const BuildConfiguration& config,  	flagsFile += ".flags";  	target_type = TargetType::DynamicLibrary; -	_language = Language::C; +	source_language = Language::C;  	for(const auto& source : config.sources)  	{  		std::filesystem::path sourceFile(source);  		if(sourceFile.extension().string() != ".c")  		{ -			_language = Language::Cpp; +			source_language = Language::Cpp;  		}  	}  } @@ -153,12 +153,7 @@ int TaskSO::runInner()  		std::cout << "LD => " << targetFile.string() << "\n";  	} -	auto tool = getConfiguration("host-cpp", "/usr/bin/g++"); -	if(language() == Language::C) -	{ -		tool = getConfiguration("host-cc", "/usr/bin/gcc"); -	} - +	auto tool = compiler();  	return execute(tool, args, settings.verbose > 0);  } @@ -202,14 +197,10 @@ std::string TaskSO::target() const  std::string TaskSO::flagsString() const  { -	std::string flagsStr; +	std::string flagsStr = compiler();  	for(const auto& flag : config.ldflags)  	{ -		if(flag != config.ldflags[0]) -		{ -			flagsStr += " "; -		} -		flagsStr += flag; +		flagsStr += " " + flag;  	}  	flagsStr += "\n";  | 
