📄 nutcomponent.c
字号:
fprintf(fp, "\n#endif\n"); fclose(fp); } else { sprintf(errtxt, "Failed to create %s", path); return -1; } } } ReleaseHeaderList (nh_root); return 0;}/*! * \brief Create build directory for Nut/OS applications. * * This routine creates Makedefs and Makerules in the specified directory. * It will also create the NutConf.mk and UserConf.mk. Except for UserConf.mk, * any existing file will be replaced. * * \param root Pointer to the root component. * \param bld_dir Pathname of the top build directory. * \param app_dir Pathname of the application build directory. * \param src_dir Pathname of the top source directory. * \param lib_dir Pathname of the directory containing the libraries. * \param mak_ext Filename extension of the platform specific Makedefs/Makerules, e.g. avr-gcc. * \param prg_ext Filename extension of the programmer specific Makedefs/Makerules, e.g. uisp-avr. * \param ifirst_dir Optional include directory. Header files will be included first * and thus may replace standard Nut/OS headers with the same name. * \param ilast_dir Optional include directory. Header files will be included last. * This parameter is typically used to specify the compilers runtime * library. Header files with the same name as Nut/OS standard headers * are ignored. * * \return 0 on success, otherwise return -1. */int CreateSampleDirectory(NUTCOMPONENT * root, const char *bld_dir, const char *app_dir, const char *src_dir, const char *lib_dir, const char *mak_ext, const char *prg_ext, const char *ifirst_dir, const char *ilast_dir){ FILE *fp; char path[255]; struct tm *ltime; time_t now; time(&now); ltime = localtime(&now); sprintf(path, "%s/NutConf.mk", app_dir); if(CreateDirectoryPath(path) == 0) { /* Create the configuration Makedefs file */ fp = fopen(path, "w"); if (fp) { fprintf(fp, "# Automatically generated on %s", asctime(ltime)); fprintf(fp, "#\n# Do not edit, modify UserConf.mk instead!\n#\n\n"); WriteMakedefLines(fp, root->nc_child); fprintf(fp, "\n\ninclude $(top_appdir)/UserConf.mk\n"); fclose(fp); } /* Create the user's Makedefs file, if it doesn't exist */ sprintf(path, "%s/UserConf.mk", app_dir); if(access(path, 0)) { fp = fopen(path, "w"); if (fp) { fprintf(fp, "# Automatically created on %s", asctime(ltime)); fprintf(fp, "#\n# You can use this file to modify values in NutConf.mk\n#\n\n"); fclose(fp); } } /* Create the application Makedefs. */ sprintf(path, "%s/Makedefs", app_dir); if ((fp = fopen(path, "w")) != 0) { fprintf(fp, "# Do not edit! Automatically generated on %s\n", asctime(ltime)); //fprintf(fp, "top_srcdir = %s\n", src_dir); if (src_dir[0] == '/' || src_dir[1] == ':') fprintf(fp, "top_srcdir = %s\n", src_dir); else if (strlen(src_dir)) fprintf(fp, "top_srcdir = ../../%s\n", src_dir); else fprintf(fp, "top_srcdir = ../..\n"); if (bld_dir[0]) fprintf(fp, "top_blddir = %s\n", bld_dir); else fprintf(fp, "top_blddir = ../..\n"); //fprintf(fp, "top_appdir = %s\n", app_dir); if (app_dir[0] == '/' || app_dir[1] == ':') fprintf(fp, "top_appdir = %s\n", app_dir); else if (strlen(app_dir)) fprintf(fp, "top_appdir = ../../%s\n", app_dir); else fprintf(fp, "top_appdir = ../..\n"); //fprintf(fp, "LIBDIR = %s\n\n", lib_dir); if (lib_dir[0] == '/' || lib_dir[1] == ':') fprintf(fp, "LIBDIR = %s\n", lib_dir); else if (strlen(lib_dir)) fprintf(fp, "LIBDIR = ../../%s\n", lib_dir); else fprintf(fp, "LIBDIR = ../..\n"); fprintf(fp, "INCFIRST=$(INCPRE)$(top_blddir)/include "); if(ifirst_dir && *ifirst_dir) { fprintf(fp, " $(INCPRE)%s", ifirst_dir); } fputc('\n', fp); if(ilast_dir && *ilast_dir) { fprintf(fp, "INCLAST = $(INCPRE)%s\n", ilast_dir); } fprintf(fp, "include $(top_appdir)/NutConf.mk\n"); fprintf(fp, "include $(top_srcdir)/app/Makedefs.%s\n", mak_ext); fprintf(fp, "include $(top_srcdir)/app/Makeburn.%s\n\n", prg_ext); fclose(fp); } else { sprintf(errtxt, "Failed to create %s", path); return -1; } /* Create the application Makerules. */ sprintf(path, "%s/Makerules", app_dir); if ((fp = fopen(path, "w")) != 0) { fprintf(fp, "# Do not edit! Automatically generated on %s\n\n", asctime(ltime)); fprintf(fp, "include $(top_srcdir)/app/Makerules.%s\n", mak_ext); fclose(fp); } else { sprintf(errtxt, "Failed to create %s", path); return -1; } } else { sprintf(errtxt, "Failed to create directory for %s", path); return -1; } return 0;}#ifdef NUT_CONFIGURE_EXEC/*! * \brief Display program usage. */void usage(void){ fputs("Usage: nutconfigure OPTIONS ACTIONS\n" "OPTIONS:\n" "-a<dir> application directory (./nutapp)\n" "-b<dir> build directory (./nutbld)\n" "-c<file> configuration file (./nut/conf/ethernut21b.conf)\n" "-i<dir> first include path ()\n" "-j<dir> last include path ()\n" "-l<dir> library directory ()\n" "-m<type> target platform (avr-gcc)\n" "-p<type> programming adapter (avr-uisp-stk500)\n" "-q quiet (verbose)\n" "-s<dir> source directory (./nut)\n" "-r<file> repository (./nut/conf/repository.nut)\n" "ACTIONS:\n" "create-buildtree\n" "create-apptree\n" , stderr);}/*! * \brief Copy a file. * * \param src_path Source pathname. * \param dst_path Destination pathname. * \param quiet If not 0, then progress and error output will be suppressed. * * \return 0 on success, -1 otherwise. */int copy_file(char *src_path, char *dst_path, int quiet){ int rc = -1; int fs; int fd; char buf[512]; int cnt; if(CreateDirectoryPath(dst_path)) { return -1; } if((fs = open(src_path, O_BINARY)) != -1) { if((fd = open(dst_path, O_RDWR | O_CREAT | O_TRUNC | O_BINARY, S_IREAD | S_IWRITE )) != -1) { for(;;) { if((cnt = read(fs, buf, sizeof(buf))) < 0) { if (!quiet) { printf("Error %d reading %s\n", errno, src_path); } break; } if (cnt == 0) { rc = 0; break; } if(write(fd, buf, cnt) != cnt) { if (!quiet) { printf("Error %d writing %s\n", errno, dst_path); } break; } } close(fd); } else { if (!quiet) { printf("Error %d opening %s\n", errno, dst_path); } } close(fs); } else { if (!quiet) { printf("Error %d opening %s\n", errno, src_path); } } return rc;}/*! * \brief Copy directory with application samples. * * \param src_dir Source directory. * \param dst_dir Desitnation directory. * \param quiet If not 0, then progress and error output will be suppressed. * * \return 0 on success, -1 otherwise. */int copy_appdir(char *src_dir, char *dst_dir, int quiet){ int rc = 0; char src_path[256]; char dst_path[256]; DIR *dir; struct dirent *dire; struct stat statbuf; if((dir = opendir(src_dir)) == NULL) { if (!quiet) { printf("Failed to scan directory %s\n", src_dir); } return -1; } if(!quiet) { printf("Copying %s\n", src_dir); } while((dire = readdir(dir)) != NULL && rc == 0) { if(dire->d_name[0] == '.' || stricmp(dire->d_name, "cvs") == 0 || strnicmp(dire->d_name, "Makeburn", 8) == 0 || strnicmp(dire->d_name, "Makedefs", 8) == 0 || strnicmp(dire->d_name, "Makerules", 9) == 0) { continue; } strcpy(dst_path, dst_dir); strcat(dst_path, "/"); strcat(dst_path, dire->d_name); strcpy(src_path, src_dir); strcat(src_path, "/"); strcat(src_path, dire->d_name); stat(src_path, &statbuf); if(statbuf.st_mode & S_IFDIR) { rc = copy_appdir(src_path, dst_path, quiet); } else if(statbuf.st_mode & S_IFREG) { copy_file(src_path, dst_path, quiet); } } closedir(dir); return rc;}/*! * \brief Running without GUI. * * All settings are passed as command line options. */int main(int argc, char **argv){ int rc = 3; int option; int quiet = 0; char *app_dir = strdup("./nutapp"); char *bld_dir = strdup("./nutbld"); char *conf_name = strdup("./nut/conf/ethernut21b.conf"); char *ifirst_dir = strdup(""); char *ilast_dir = strdup(""); char *lib_dir = strdup(""); char *mak_ext = strdup("avr-gcc"); char *prg_ext = strdup("avr-uisp-stk500"); char *src_dir = strdup("./nut"); char *repo_name = strdup("./nut/conf/repository.nut"); NUTREPOSITORY *repo; NUTCOMPONENT *root; while((option = getopt(argc, argv, "a:b:c:i:j:l:m:p:s:r:v?")) != EOF) { switch(option) { case 'a': free(app_dir); app_dir = strdup(optarg); break; case 'b': free(bld_dir); bld_dir = strdup(optarg); break; case 'c': free(conf_name); conf_name = strdup(optarg); break; case 'i': free(ifirst_dir); ifirst_dir = strdup(optarg); break; case 'j': free(ilast_dir); ilast_dir = strdup(optarg); break; case 'l': free(lib_dir); lib_dir = strdup(optarg); break; case 'm': free(mak_ext); mak_ext = strdup(optarg); break; case 'p': free(prg_ext); prg_ext = strdup(optarg); break; case 'q': quiet = 1; break; case 's': free(src_dir); src_dir = strdup(optarg); break; case 'r': free(repo_name); repo_name = strdup(optarg); break; default: usage(); return 1; } } argc -= optind; argv += optind; if (*lib_dir == '\0') { free(lib_dir); lib_dir = malloc(strlen(bld_dir) + 5); strcpy(lib_dir, bld_dir); strcat(lib_dir, "/lib"); } if(!quiet) { printf("nutconfigure %s\n", NUT_CONFIGURE_VERSION); printf("Loading %s...", repo_name); } /* * The repository is the top Lua script. */ if ((repo = OpenRepository(repo_name)) != NULL) { /* Load the component tree. */ if ((root = LoadComponents(repo)) != NULL) { if(!quiet) { printf("OK\nLoading %s...", conf_name); } /* Load the hardware specific configuration file. */ if (ConfigureComponents(repo, root, conf_name)) { if(!quiet) { printf("%s\n", GetScriptErrorString()); } } else { if(!quiet) { printf("OK\n"); } RefreshComponents(root); if(argc == 0) { if(!quiet) { printf("Nothing to do\n"); } } else if(strcmp(argv[0], "create-buildtree") == 0) { if(!quiet) { printf("Creating Makefiles for %s in %s...", mak_ext, bld_dir); } if (CreateMakeFiles(root, bld_dir, src_dir, mak_ext, ifirst_dir, ilast_dir, lib_dir)) { if(!quiet) { printf("failed\n"); } } else { if(!quiet) { printf("OK\nCreating header files in %s...", bld_dir); } if (CreateHeaderFiles(root, bld_dir)) { if(!quiet) { printf("failed\n"); } } else { if(!quiet) { printf("OK\n"); } rc = 0; } } } else if(strcmp(argv[0], "c
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -