📄 build.cxx
字号:
while (index = output.find (old_substring, index), std::string::npos != index) { output.replace (index, old_substring.size (), new_substring); } return output;}// resolve tokens in custom make rule targets and dependenciesstd::string resolve_tokens (const std::string input) { std::string output = input; output = replace_substr (output, "<PREFIX>", "$(PREFIX)"); output = replace_substr (output, "<PACKAGE>", "$(REPOSITORY)/$(PACKAGE)"); return output;}// create the makefile for a loadablebool generate_makefile (const CdlConfiguration config, const CdlBuildInfo_Loadable & info, const std::string install_tree, const std::string filename) { unsigned int count; unsigned int library; // obtain the command prefix std::string command_prefix = get_flags (config, NULL, "COMMAND_PREFIX"); if (! command_prefix.empty ()) { // if there is a command prefix command_prefix += '-'; // add a trailing hyphen } // generate the prefix for archived objects unsigned int final_separator = 0; // the index of the last directory separator std::string object_prefix = info.directory; // start with the loadable directory for (count = 0; count < object_prefix.size (); count++) { // for each char if ('/' == object_prefix [count]) { // if the char is a directory separator object_prefix [count] = '_'; // replace the char with an underscore final_separator = count; } } object_prefix.resize (final_separator); // remove the version directory // open the makefile FILE * stream = fopen (filename.c_str (), "wt"); if (stream == NULL) // if the file could not be opened return false; // generate the header fprintf (stream, makefile_header.c_str ()); // generate the global variables fprintf (stream, "export REPOSITORY := %s\n", cygpath (config->get_database ()->get_component_repository ()).c_str ()); fprintf (stream, "export PREFIX := %s\n", cygpath (install_tree).c_str ()); fprintf (stream, "export COMMAND_PREFIX := %s\n", command_prefix.c_str ()); fprintf (stream, "export CC := $(COMMAND_PREFIX)gcc\n"); fprintf (stream, "export OBJCOPY := $(COMMAND_PREFIX)objcopy\n");#if defined(_WIN32) || defined(__CYGWIN__) fprintf (stream, "export HOST := CYGWIN\n");#else fprintf (stream, "export HOST := UNIX\n");#endif fprintf (stream, "export AR := $(COMMAND_PREFIX)ar\n\n"); // generate the package variables fprintf (stream, "PACKAGE := %s\n", info.directory.c_str ()); fprintf (stream, "OBJECT_PREFIX := %s\n", object_prefix.c_str ()); fprintf (stream, "CFLAGS := %s\n", get_flags (config, &info, "CFLAGS").c_str ()); fprintf (stream, "LDFLAGS := %s\n", get_flags (config, &info, "LDFLAGS").c_str ()); fprintf (stream, "VPATH := $(REPOSITORY)/$(PACKAGE)\n"); fprintf (stream, "INCLUDE_PATH := $(INCLUDE_PATH) -I$(PREFIX)/include $(foreach dir,$(VPATH),-I$(dir) -I$(dir)/src -I$(dir)/tests) -I.\n"); fprintf (stream, "MLT := $(wildcard $(REPOSITORY)/$(PACKAGE)/include/pkgconf/mlt*.ldi $(REPOSITORY)/$(PACKAGE)/include/pkgconf/mlt*.h)\n"); fprintf (stream, "TESTS := %s\n\n", get_tests (config, info).c_str ()); // create a vector of libraries std::vector <std::string> library_vector; for (count = 0; count < info.compiles.size (); count++) { // for each compilable file const std::string & library = info.compiles [count].library; if (library_vector.end () == std::find (library_vector.begin (), library_vector.end (), library)) { // if a new library library_vector.push_back (library); // add the library to the vector } } for (count = 0; count < info.objects.size (); count++) { // for each object file const std::string & library = info.objects [count].library; if (library_vector.end () == std::find (library_vector.begin (), library_vector.end (), library)) { // if a new library library_vector.push_back (library); // add the library to the vector } } for (count = 0; count < info.make_objects.size (); count++) { // for each make object const std::string & library = info.make_objects [count].library; if (library_vector.end () == std::find (library_vector.begin (), library_vector.end (), library)) { // if a new library library_vector.push_back (library); // add the library to the vector } } // generate the default rule fprintf (stream, "build: headers"); for (library = 0; library < library_vector.size (); library++) { // for each library fprintf (stream, " %s.stamp", library_vector [library].c_str ()); } fprintf (stream, "\n\n"); // generate library rules for (library = 0; library < library_vector.size (); library++) { // for each library fprintf (stream, "LIBRARY := %s\n", library_vector [library].c_str ()); fprintf (stream, "COMPILE :="); for (count = 0; count < info.compiles.size (); count++) { // for each compilable file if (library_vector [library] == info.compiles [count].library) { // if the file and library are related fprintf (stream, " %s", info.compiles [count].source.c_str ()); } } for (count = 0; count < info.objects.size (); count++) { // for each object file if (library_vector [library] == info.objects [count].library) { // if the file and library are related fprintf (stream, " %s", info.objects [count].object.c_str ()); } } for (count = 0; count < info.make_objects.size (); count++) { // for each make object if (library_vector [library] == info.make_objects [count].library) { // if the object and library are related fprintf (stream, " %s", info.make_objects [count].object.c_str ()); } } fprintf (stream, "\nOBJECTS := $(COMPILE:.cxx=.o.d)\n"); fprintf (stream, "OBJECTS := $(OBJECTS:.c=.o.d)\n"); fprintf (stream, "OBJECTS := $(OBJECTS:.S=.o.d)\n\n"); fprintf (stream, "$(LIBRARY).stamp: $(OBJECTS)\n"); fprintf (stream, "\t$(AR) rcs $(PREFIX)/lib/$(@:.stamp=) $(foreach obj,$?,$(dir $(obj))$(OBJECT_PREFIX)_$(notdir $(obj:.o.d=.o)))\n"); fprintf (stream, "\t@cat $^ > $(@:.stamp=.deps)\n"); fprintf (stream, "\t@touch $@\n\n"); } // generate make objects rules for (count = 0; count < info.make_objects.size (); count++) { // for each make object fprintf (stream, "%s: %s\n%s\n", info.make_objects [count].object.c_str (), info.make_objects [count].deps.c_str (), tab_indent (info.make_objects [count].rules).c_str ()); } // generate makes rules for (count = 0; count < info.makes.size (); count++) { // for each make fprintf (stream, "%s: $(wildcard %s)\n%s\n", resolve_tokens (info.makes [count].target).c_str (), resolve_tokens (info.makes [count].deps).c_str (), tab_indent (info.makes [count].rules).c_str ()); } // generate header rules fprintf (stream, "headers: mlt_headers"); for (count = 0; count < info.headers.size (); count++) { // for each header fprintf (stream, " $(PREFIX)/include/%s", info.headers [count].destination.c_str ()); } fprintf (stream, "\n\n"); for (count = 0; count < info.headers.size (); count++) { // for each header fprintf (stream, "$(PREFIX)/include/%s: $(REPOSITORY)/$(PACKAGE)/%s\n", info.headers [count].destination.c_str (), info.headers [count].source.c_str ());#if (defined(_WIN32) || defined(__CYGWIN__)) && (ECOS_USE_CYGDRIVE > 0) fprintf (stream, "ifeq ($(HOST),CYGWIN)\n"); fprintf (stream, "\t@mkdir -p `cygpath -w \"$(dir $@)\" | sed \"s@\\\\\\\\\\\\\\\\@/@g\"`\n"); fprintf (stream, "else\n"); fprintf (stream, "\t@mkdir -p $(dir $@)\n"); fprintf (stream, "endif\n");#else // This prevents older versions of mkdir failing fprintf (stream, "\t@mkdir -p $(dir $@)\n");#endif fprintf (stream, "\t@cp $< $@\n"); fprintf (stream, "\t@chmod u+w $@\n\n"); } // include default rules fprintf (stream, "include $(REPOSITORY)/pkgconf/rules.mak\n\n"); // close the makefile return (0 == fclose (stream));}// a structure and operator for sorting custom make rules by prioritystruct info_make { std::vector <CdlBuildInfo_Loadable>::const_iterator loadable; unsigned int make;};bool operator < (info_make op1, info_make op2) { return op1.loadable->makes [op1.make].priority < op2.loadable->makes [op2.make].priority;}// create the top-level makefilebool generate_toplevel_makefile (const CdlConfiguration config, const std::string install_tree, const std::string filename) { unsigned int loadable; unsigned int make; // obtain the command prefix std::string command_prefix = get_flags (config, NULL, "COMMAND_PREFIX"); if (! command_prefix.empty ()) { // if there is a command prefix command_prefix += '-'; // add a trailing hyphen } // obtain build information from the specified configuration CdlBuildInfo build_info; config->get_build_info (build_info); std::vector <CdlBuildInfo_Loadable> info_vector = build_info.entries; // create a vector of makes and sort them by priority std::vector <info_make> info_make_vector; for (std::vector <CdlBuildInfo_Loadable>::iterator info = info_vector.begin (); info != info_vector.end (); info++) { // for each buildable loaded package for (unsigned int count = 0; count < info->makes.size (); count++) { // for each make info_make make_info = {info, count}; info_make_vector.push_back (make_info); } } std::sort (info_make_vector.begin (), info_make_vector.end ()); // open the makefile FILE * stream = fopen (filename.c_str (), "wt"); if (stream == NULL) // if the file could not be opened return false; // generate the header fprintf (stream, makefile_header.c_str ()); // generate the variables fprintf (stream, "export REPOSITORY := %s\n", cygpath (config->get_database ()->get_component_repository ()).c_str ());#if defined(_WIN32) || defined(__CYGWIN__) fprintf (stream, "export HOST := CYGWIN\n");#else fprintf (stream, "export HOST := UNIX\n");#endif fprintf (stream, "export PREFIX := %s\n", cygpath (install_tree).c_str ()); fprintf (stream, "export COMMAND_PREFIX := %s\n", command_prefix.c_str ()); fprintf (stream, "export CC := $(COMMAND_PREFIX)gcc\n"); fprintf (stream, "export OBJCOPY := $(COMMAND_PREFIX)objcopy\n"); fprintf (stream, "export AR := $(COMMAND_PREFIX)ar\n\n"); // generate the makefile contents fprintf (stream, ".PHONY: default build clean tests headers\n\n"); fprintf (stream, "build: headers $(PREFIX)/include/pkgconf/ecos.mak\n"); for (make = 0; make < info_make_vector.size (); make++) { // for each make if (info_make_vector [make].loadable->makes [info_make_vector [make].make].priority < 100) { // if priority higher than default complilation fprintf (stream, "\t$(MAKE) -r -C %s %s\n", info_make_vector [make].loadable->directory.c_str (), resolve_tokens (info_make_vector [make].loadable->makes [info_make_vector [make].make].target).c_str ()); } } for (loadable = 0; loadable < info_vector.size (); loadable++) { // for each buildable loaded package const std::string source_path = info_vector [loadable].directory; fprintf (stream, "\t$(MAKE) -r -C %s $@\n", source_path.c_str ()); } for (make = 0; make < info_make_vector.size (); make++) { // for each make if (info_make_vector [make].loadable->makes [info_make_vector [make].make].priority >= 100) { // if priority lower than or equal to default complilation fprintf (stream, "\t$(MAKE) -r -C %s %s\n", info_make_vector [make].loadable->directory.c_str (), resolve_tokens (info_make_vector [make].loadable->makes [info_make_vector [make].make].target).c_str ()); } } fprintf (stream, "\t@echo $@ finished\n\n"); fprintf (stream, "clean:\n"); for (loadable = 0; loadable < info_vector.size (); loadable++) { // for each buildable loaded package const std::string source_path = info_vector [loadable].directory; fprintf (stream, "\t$(MAKE) -r -C %s $@\n", source_path.c_str ()); } fprintf (stream, "\t@echo $@ finished\n\n"); fprintf (stream, "tests: build\n"); for (loadable = 0; loadable < info_vector.size (); loadable++) { // for each buildable loaded package const std::string source_path = info_vector [loadable].directory; fprintf (stream, "\t$(MAKE) -r -C %s $@\n", source_path.c_str ()); } fprintf (stream, "\t@echo $@ finished\n\n"); fprintf (stream, "headers:\n"); for (loadable = 0; loadable < info_vector.size (); loadable++) { // for each buildable loaded package const std::string source_path = info_vector [loadable].directory; fprintf (stream, "\t$(MAKE) -r -C %s $@\n", source_path.c_str ()); } fprintf (stream, "\t@echo $@ finished\n\n"); fprintf (stream, "$(PREFIX)/include/pkgconf/ecos.mak: makefile\n"); fprintf (stream, "\t@echo 'ECOS_GLOBAL_CFLAGS = %s' > $@\n", get_flags (config, NULL, "CFLAGS").c_str ()); fprintf (stream, "\t@echo 'ECOS_GLOBAL_LDFLAGS = %s' >> $@\n", get_flags (config, NULL, "LDFLAGS").c_str ()); fprintf (stream, "\t@echo 'ECOS_COMMAND_PREFIX = $(COMMAND_PREFIX)' >> $@\n\n"); // close the makefile return (0 == fclose (stream));}// generates the directory structure for the build and install treesbool generate_build_tree (const CdlConfiguration config, const std::string build_tree, const std::string install_tree /* = "" */ ) {#if defined(_WIN32) || defined(__CYGWIN__) // convert backslash directory separators to forward slashes under Win32 const std::string build_dir = replace_char (build_tree, '\\', '/'); const std::string install_dir = install_tree.empty () ? build_dir + "/install" : replace_char (install_tree, '\\', '/');#else const std::string build_dir = build_tree; const std::string install_dir = install_tree.empty () ? build_dir + "/install" : install_tree;#endif // create build and install directories to ensure they are in writable locations if (! create_directory (build_dir)) return false; if (! create_directory (install_dir + "/lib")) return false; if (! create_directory (install_dir + "/include/pkgconf")) return false; // obtain build information from the specified configuration CdlBuildInfo build_info; config->get_build_info (build_info); std::vector <CdlBuildInfo_Loadable> info_vector = build_info.entries; for (unsigned int loadable = 0; loadable < info_vector.size (); loadable++) { // for each buildable loaded package const std::string build_dir_loadable = build_dir + "/" + info_vector [loadable].directory; // create loadable directory in build tree if (! create_directory (build_dir_loadable)) return false; // generate makefile generate_makefile (config, info_vector [loadable], install_dir, build_dir_loadable + "/makefile"); } generate_toplevel_makefile (config, install_dir, build_dir + "/makefile"); return true;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -