You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucy.apache.org by nw...@apache.org on 2013/02/06 23:08:26 UTC
[lucy-commits] [1/4] git commit: refs/heads/c-bindings-cfc - Regenerate charmonizer.c
Regenerate charmonizer.c
Project: http://git-wip-us.apache.org/repos/asf/lucy/repo
Commit: http://git-wip-us.apache.org/repos/asf/lucy/commit/4cbd991e
Tree: http://git-wip-us.apache.org/repos/asf/lucy/tree/4cbd991e
Diff: http://git-wip-us.apache.org/repos/asf/lucy/diff/4cbd991e
Branch: refs/heads/c-bindings-cfc
Commit: 4cbd991ec374b48505884a7e1dc7862cd73af1fa
Parents: e65019c
Author: Nick Wellnhofer <we...@aevum.de>
Authored: Sun Dec 23 02:25:15 2012 +0100
Committer: Nick Wellnhofer <we...@aevum.de>
Committed: Wed Feb 6 23:03:54 2013 +0100
----------------------------------------------------------------------
clownfish/compiler/common/charmonizer.c | 753 +++++++++++++++++++++++++-
clownfish/runtime/common/charmonizer.c | 605 ++++++++++++++++++++-
common/charmonizer.c | 605 ++++++++++++++++++++-
3 files changed, 1951 insertions(+), 12 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/lucy/blob/4cbd991e/clownfish/compiler/common/charmonizer.c
----------------------------------------------------------------------
diff --git a/clownfish/compiler/common/charmonizer.c b/clownfish/compiler/common/charmonizer.c
index 21bd6d1..70775c3 100644
--- a/clownfish/compiler/common/charmonizer.c
+++ b/clownfish/compiler/common/charmonizer.c
@@ -117,6 +117,18 @@ chaz_CC_gcc_version();
int
chaz_CC_compiler_is_msvc(void);
+const char*
+chaz_CC_link_command();
+
+const char*
+chaz_CC_link_flags();
+
+const char*
+chaz_CC_link_shared_obj_flag();
+
+const char*
+chaz_CC_link_output_flag();
+
#endif /* H_CHAZ_COMPILER */
@@ -325,6 +337,70 @@ chaz_HeadCheck_contains_member(const char *struct_name, const char *member,
/***************************************************************************/
+#line 21 "src/Charmonizer/Core/Make.h"
+/* Charmonizer/Core/Compiler.h
+ */
+
+#ifndef H_CHAZ_MAKE
+#define H_CHAZ_MAKE
+
+typedef struct chaz_MakeFile chaz_MakeFile;
+typedef struct chaz_MakeVar chaz_MakeVar;
+typedef struct chaz_MakeRule chaz_MakeRule;
+
+typedef void (*chaz_Make_list_files_callback_t)(char *file, void *context);
+
+void
+chaz_Make_list_files(const char *dir, const char *ext,
+ chaz_Make_list_files_callback_t callback, void *context);
+
+chaz_MakeFile*
+chaz_MakeFile_new();
+
+chaz_MakeVar*
+chaz_MakeFile_add_var(chaz_MakeFile *makefile, const char *name,
+ const char *value);
+
+chaz_MakeRule*
+chaz_MakeFile_add_rule(chaz_MakeFile *makefile, const char *target,
+ const char *prereq);
+
+void
+chaz_MakeFile_add_to_cleanup(chaz_MakeFile *makefile, const char *target);
+
+void
+chaz_MakeFile_add_exe(chaz_MakeFile *makefile, const char *exe,
+ const char *objects);
+
+void
+chaz_MakeFile_add_shared_obj(chaz_MakeFile *makefile, const char *shared_obj,
+ const char *objects);
+
+void
+chaz_MakeFile_write(chaz_MakeFile *makefile);
+
+void
+chaz_MakeVar_append(chaz_MakeVar *var, const char *element);
+
+void
+chaz_MakeRule_add_target(chaz_MakeRule *rule, const char *target);
+
+void
+chaz_MakeRule_add_prereq(chaz_MakeRule *rule, const char *prereq);
+
+void
+chaz_MakeRule_add_command(chaz_MakeRule *rule, const char *command);
+
+void
+chaz_MakeRule_add_command_make(chaz_MakeRule *rule, const char *dir,
+ const char *target);
+
+#endif /* H_CHAZ_MAKE */
+
+
+
+/***************************************************************************/
+
#line 21 "src/Charmonizer/Core/OperatingSystem.h"
/* Charmonizer/Core/OperatingSystem.h - abstract an operating system down to a few
* variables.
@@ -333,6 +409,9 @@ chaz_HeadCheck_contains_member(const char *struct_name, const char *member,
#ifndef H_CHAZ_OPER_SYS
#define H_CHAZ_OPER_SYS
+#define CHAZ_OS_POSIX 1
+#define CHAZ_OS_CMD_EXE 2
+
/* Safely remove a file named [name]. Needed because of Windows quirks.
* Returns true on success, false on failure.
*/
@@ -358,6 +437,11 @@ chaz_OS_run_redirected(const char *command, const char *path);
int
chaz_OS_run_local_redirected(const char *command, const char *path);
+/* Run a command and return the output from stdout.
+ */
+char*
+chaz_OS_run_and_capture(const char *command, size_t *output_len);
+
/* Attempt to create a directory.
*/
void
@@ -368,6 +452,17 @@ chaz_OS_mkdir(const char *filepath);
void
chaz_OS_rmdir(const char *filepath);
+/* Return the operating system name.
+ */
+const char*
+chaz_OS_name(void);
+
+int
+chaz_OS_is_darwin(void);
+
+int
+chaz_OS_is_cygwin(void);
+
/* Return the extension for an executable on this system.
*/
const char*
@@ -378,11 +473,21 @@ chaz_OS_exe_ext(void);
const char*
chaz_OS_obj_ext(void);
+/* Return the extension for a shared object on this system.
+ */
+const char*
+chaz_OS_shared_obj_ext(void);
+
/* Return the equivalent of /dev/null on this system.
*/
const char*
chaz_OS_dev_null(void);
+/* Return the shell type of this system.
+ */
+int
+chaz_OS_shell_type(void);
+
/* Initialize the Charmonizer/Core/OperatingSystem module.
*/
void
@@ -1172,6 +1277,45 @@ chaz_CC_compiler_is_msvc(void) {
return !!chaz_CC.intval__MSC_VER;
}
+const char*
+chaz_CC_link_command() {
+ if (chaz_CC.intval__MSC_VER) {
+ return "link";
+ }
+ else {
+ return chaz_CC.cc_command;
+ }
+}
+
+const char*
+chaz_CC_link_flags() {
+ return "";
+}
+
+const char*
+chaz_CC_link_shared_obj_flag() {
+ if (chaz_CC.intval__MSC_VER) {
+ return "/DLL";
+ }
+ else if (chaz_OS_is_darwin()) {
+ return "-dynamiclib ";
+ }
+ else {
+ return "-shared";
+ }
+}
+
+const char*
+chaz_CC_link_output_flag() {
+ if (chaz_CC.intval__MSC_VER) {
+ return "/OUT:";
+ }
+ else {
+ return "-o ";
+ }
+}
+
+
/***************************************************************************/
@@ -2277,25 +2421,417 @@ chaz_HeadCheck_maybe_add_to_cache(const char *header_name, int exists) {
/***************************************************************************/
+#line 17 "src/Charmonizer/Core/Make.c"
+#include <string.h>
+/* #include "Charmonizer/Core/Make.h" */
+/* #include "Charmonizer/Core/Compiler.h" */
+/* #include "Charmonizer/Core/OperatingSystem.h" */
+/* #include "Charmonizer/Core/Util.h" */
+
+struct chaz_MakeVar {
+ char *name;
+ char *value;
+ size_t num_elements;
+};
+
+struct chaz_MakeRule {
+ char *targets;
+ char *prereqs;
+ char *commands;
+};
+
+struct chaz_MakeFile {
+ chaz_MakeVar **vars;
+ size_t num_vars;
+ chaz_MakeRule **rules;
+ size_t num_rules;
+ char **cleanups;
+ size_t num_cleanups;
+};
+
+chaz_MakeFile*
+chaz_MakeFile_new() {
+ chaz_MakeFile *makefile = (chaz_MakeFile*)malloc(sizeof(chaz_MakeFile));
+
+ makefile->vars = (chaz_MakeVar**)malloc(sizeof(chaz_MakeVar*));
+ makefile->vars[0] = NULL;
+ makefile->num_vars = 0;
+
+ makefile->rules = (chaz_MakeRule**)malloc(sizeof(chaz_MakeRule*));
+ makefile->rules[0] = NULL;
+ makefile->num_rules = 0;
+
+ makefile->cleanups = (char**)malloc(sizeof(char*));
+ makefile->cleanups[0] = NULL;
+ makefile->num_cleanups = 0;
+
+ return makefile;
+}
+
+chaz_MakeVar*
+chaz_MakeFile_add_var(chaz_MakeFile *makefile, const char *name,
+ const char *value) {
+ chaz_MakeVar *var = (chaz_MakeVar*)malloc(sizeof(chaz_MakeVar));
+ chaz_MakeVar **vars = makefile->vars;
+ size_t num_vars = makefile->num_vars + 1;
+
+ var->name = chaz_Util_strdup(name);
+ var->value = chaz_Util_strdup("");
+ var->num_elements = 0;
+
+ if (value) { chaz_MakeVar_append(var, value); }
+
+ vars = (chaz_MakeVar**)realloc(vars,
+ (num_vars + 1) * sizeof(chaz_MakeVar*));
+ vars[num_vars-1] = var;
+ vars[num_vars] = NULL;
+ makefile->vars = vars;
+ makefile->num_vars = num_vars;
+
+ return var;
+}
+
+chaz_MakeRule*
+chaz_MakeFile_add_rule(chaz_MakeFile *makefile, const char *target,
+ const char *prereq) {
+ chaz_MakeRule *rule = (chaz_MakeRule*)malloc(sizeof(chaz_MakeRule));
+ chaz_MakeRule **rules = makefile->rules;
+ size_t num_rules = makefile->num_rules + 1;
+
+ rule->targets = NULL;
+ rule->prereqs = NULL;
+ rule->commands = NULL;
+
+ if (target) { chaz_MakeRule_add_target(rule, target); }
+ if (prereq) { chaz_MakeRule_add_prereq(rule, prereq); }
+
+ rules = (chaz_MakeRule**)realloc(rules,
+ (num_rules + 1) * sizeof(chaz_MakeRule*));
+ rules[num_rules-1] = rule;
+ rules[num_rules] = NULL;
+ makefile->rules = rules;
+ makefile->num_rules = num_rules;
+
+ return rule;
+}
+
+void
+chaz_MakeFile_add_to_cleanup(chaz_MakeFile *makefile, const char *target) {
+ char *cleanup = chaz_Util_strdup(target);
+ char **cleanups = makefile->cleanups;
+ size_t num_cleanups = makefile->num_cleanups + 1;
+
+ cleanups = (char**)realloc(cleanups, (num_cleanups + 1) * sizeof(char*));
+ cleanups[num_cleanups-1] = cleanup;
+ cleanups[num_cleanups] = NULL;
+ makefile->cleanups = cleanups;
+ makefile->num_cleanups = num_cleanups;
+}
+
+void
+chaz_MakeFile_add_exe(chaz_MakeFile *makefile, const char *exe,
+ const char *objects) {
+ const char *pattern = "%s %s %s %s%s";
+ const char *link = chaz_CC_link_command();
+ const char *link_flags = chaz_CC_link_flags();
+ const char *output_flag = chaz_CC_link_output_flag();
+ chaz_MakeRule *rule;
+ char *command;
+ size_t size;
+
+ rule = chaz_MakeFile_add_rule(makefile, exe, objects);
+
+ size = strlen(pattern)
+ + strlen(link)
+ + strlen(link_flags)
+ + strlen(objects)
+ + strlen(output_flag)
+ + strlen(exe)
+ + 50;
+ command = (char*)malloc(size);
+ sprintf(command, pattern, link, link_flags, objects, output_flag, exe);
+ chaz_MakeRule_add_command(rule, command);
+
+ chaz_MakeFile_add_to_cleanup(makefile, exe);
+}
+
+void
+chaz_MakeFile_add_shared_obj(chaz_MakeFile *makefile, const char *shared_obj,
+ const char *objects) {
+ const char *pattern = "%s %s %s %s %s%s";
+ const char *link = chaz_CC_link_command();
+ const char *shobj_flags = chaz_CC_link_shared_obj_flag();
+ const char *link_flags = chaz_CC_link_flags();
+ const char *output_flag = chaz_CC_link_output_flag();
+ chaz_MakeRule *rule;
+ char *command;
+ size_t size;
+
+ rule = chaz_MakeFile_add_rule(makefile, shared_obj, objects);
+
+ size = strlen(pattern)
+ + strlen(link)
+ + strlen(shobj_flags)
+ + strlen(link_flags)
+ + strlen(objects)
+ + strlen(output_flag)
+ + strlen(shared_obj)
+ + 50;
+ command = (char*)malloc(size);
+ sprintf(command, pattern, link, shobj_flags, link_flags, objects,
+ output_flag, shared_obj);
+ chaz_MakeRule_add_command(rule, command);
+
+ chaz_MakeFile_add_to_cleanup(makefile, shared_obj);
+}
+
+void
+chaz_MakeFile_write(chaz_MakeFile *makefile) {
+ FILE *file;
+ size_t i;
+
+ file = fopen("Makefile", "w");
+ if (!file) {
+ chaz_Util_die("Can't open Makefile\n");
+ }
+
+ for (i = 0; makefile->vars[i]; i++) {
+ chaz_MakeVar *var = makefile->vars[i];
+ fprintf(file, "%s = %s\n", var->name, var->value);
+ }
+ fprintf(file, "\n");
+
+ for (i = 0; makefile->rules[i]; i++) {
+ chaz_MakeRule *rule = makefile->rules[i];
+ fprintf(file, "%s :", rule->targets);
+ if (rule->prereqs) {
+ fprintf(file, " %s", rule->prereqs);
+ }
+ fprintf(file, "\n");
+ if (rule->commands) {
+ fprintf(file, "%s", rule->commands);
+ }
+ fprintf(file, "\n");
+ }
+
+ if (makefile->cleanups[0]) {
+ fprintf(file, "clean :\n\trm -f");
+ for (i = 0; makefile->cleanups[i]; i++) {
+ char *cleanup = makefile->cleanups[i];
+ fprintf(file, " \\\n\t %s", cleanup);
+ }
+ fprintf(file, "\n\n");
+ }
+
+ fprintf(file, "distclean : clean\n");
+ fprintf(file, "\trm -f charmonizer charmony.h Makefile\n\n");
+
+ fclose(file);
+}
+
+void
+chaz_MakeVar_append(chaz_MakeVar *var, const char *element) {
+ char *value;
+
+ if (element[0] == '\0') { return; }
+
+ if (var->num_elements == 0) {
+ value = chaz_Util_strdup(element);
+ }
+ else {
+ value = (char*)malloc(strlen(var->value) + strlen(element) + 20);
+
+ if (var->num_elements == 1) {
+ sprintf(value, "\\\n %s \\\n %s", var->value, element);
+ }
+ else {
+ sprintf(value, "%s \\\n %s", var->value, element);
+ }
+ }
+
+ free(var->value);
+ var->value = value;
+ var->num_elements++;
+}
+
+void
+chaz_MakeRule_add_target(chaz_MakeRule *rule, const char *target) {
+ char *targets;
+
+ if (!rule->targets) {
+ targets = chaz_Util_strdup(target);
+ }
+ else {
+ targets = (char*)malloc(strlen(rule->targets) + strlen(target) + 20);
+ sprintf(targets, "%s %s", rule->targets, target);
+ free(rule->targets);
+ }
+
+ rule->targets = targets;
+}
+
+void
+chaz_MakeRule_add_prereq(chaz_MakeRule *rule, const char *prereq) {
+ char *prereqs;
+
+ if (!rule->prereqs) {
+ prereqs = chaz_Util_strdup(prereq);
+ }
+ else {
+ prereqs = (char*)malloc(strlen(rule->prereqs) + strlen(prereq) + 20);
+ sprintf(prereqs, "%s %s", rule->prereqs, prereq);
+ free(rule->prereqs);
+ }
+
+ rule->prereqs = prereqs;
+}
+
+void
+chaz_MakeRule_add_command(chaz_MakeRule *rule, const char *command) {
+ char *commands;
+
+ if (!rule->commands) {
+ commands = (char*)malloc(strlen(command) + 20);
+ sprintf(commands, "\t%s\n", command);
+ }
+ else {
+ commands = (char*)malloc(strlen(rule->commands) + strlen(command) + 20);
+ sprintf(commands, "%s\t%s\n", rule->commands, command);
+ free(rule->commands);
+ }
+
+ rule->commands = commands;
+}
+
+void
+chaz_MakeRule_add_command_make(chaz_MakeRule *rule, const char *dir,
+ const char *target) {
+ char *command;
+
+ if (!target) {
+ command = (char*)malloc(strlen(dir) + 20);
+ sprintf(command, "make -C %s", dir);
+ }
+ else {
+ command = (char*)malloc(strlen(dir) + strlen(target) + 20);
+ sprintf(command, "make -C %s %s", dir, target);
+ }
+
+ chaz_MakeRule_add_command(rule, command);
+
+ free(command);
+}
+
+void
+chaz_Make_list_files(const char *dir, const char *ext,
+ chaz_Make_list_files_callback_t callback, void *context) {
+ int shell_type = chaz_OS_shell_type();
+ const char *pattern;
+ char *command;
+ char *list;
+ char *prefix;
+ char *file;
+ size_t command_size;
+ size_t list_len;
+ size_t prefix_len;
+
+ /* List files using shell. */
+
+ if (shell_type == CHAZ_OS_POSIX) {
+ pattern = "find %s -name '*.%s' -type f";
+ }
+ else if (shell_type == CHAZ_OS_CMD_EXE) {
+ pattern = "dir %s\\*.%s /s /b /a-d";
+ }
+ else {
+ chaz_Util_die("Unknown shell type %d", shell_type);
+ }
+
+ command_size = strlen(pattern) + strlen(dir) + strlen(ext) + 10;
+ command = (char*)malloc(command_size);
+ sprintf(command, pattern, dir, ext);
+ list = chaz_OS_run_and_capture(command, &list_len);
+ free(command);
+ if (!list) {
+ chaz_Util_die("Failed to list files in '%s'", dir);
+ }
+ list[list_len-1] = 0;
+
+ /* Find directory prefix to strip from files */
+
+ if (shell_type == CHAZ_OS_POSIX) {
+ prefix_len = strlen(dir);
+ prefix = (char*)malloc(prefix_len + 2);
+ memcpy(prefix, dir, prefix_len);
+ prefix[prefix_len++] = '/';
+ prefix[prefix_len] = '\0';
+ }
+ else {
+ char *output;
+ size_t output_len;
+
+ /* 'dir /s' returns absolute paths, so we have to find the absolute
+ * path of the directory. This is done by using the variable
+ * substitution feature of the 'for' command.
+ */
+ pattern = "for %%I in (%s) do echo %%~fI";
+ command_size = strlen(pattern) + strlen(dir) + 10;
+ command = (char*)malloc(command_size);
+ sprintf(command, pattern, dir);
+ output = chaz_OS_run_and_capture(command, &output_len);
+ free(command);
+ if (!output) { chaz_Util_die("Failed to find absolute path"); }
+
+ prefix_len = strcspn(output, "\r\n");
+ prefix = (char*)malloc(prefix_len + 2);
+ memcpy(prefix, output, prefix_len);
+ prefix[prefix_len++] = '\\';
+ prefix[prefix_len] = '\0';
+ free(output);
+ }
+
+ /* Iterate file list and invoke callback. */
+
+ for (file = strtok(list, "\r\n"); file; file = strtok(NULL, "\r\n")) {
+ if (strlen(file) <= prefix_len
+ || memcmp(file, prefix, prefix_len) != 0
+ ) {
+ chaz_Util_die("Unexpected file name");
+ }
+
+ callback(file + prefix_len, context);
+ }
+
+ free(prefix);
+ free(list);
+}
+
+
+/***************************************************************************/
+
#line 17 "src/Charmonizer/Core/OperatingSystem.c"
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
+#include <ctype.h>
/* #include "Charmonizer/Core/Compiler.h" */
/* #include "Charmonizer/Core/Util.h" */
/* #include "Charmonizer/Core/ConfWriter.h" */
/* #include "Charmonizer/Core/OperatingSystem.h" */
+#define CHAZ_OS_TARGET_PATH "_charmonizer_target"
+#define CHAZ_OS_NAME_MAX 31
+
static struct {
+ char name[CHAZ_OS_NAME_MAX+1];
char dev_null[20];
char exe_ext[5];
char obj_ext[5];
+ char shared_obj_ext[7];
char local_command_start[3];
int shell_type;
-} chaz_OS = { "", "", "", "", 0 };
-#define CHAZ_OS_POSIX 1
-#define CHAZ_OS_CMD_EXE 2
+} chaz_OS = { "", "", "", "", "", "", 0 };
void
chaz_OS_init(void) {
@@ -2309,16 +2845,43 @@ chaz_OS_init(void) {
/* Detect shell based on whether the bitbucket is "/dev/null" or "nul". */
if (chaz_Util_can_open_file("/dev/null")) {
+ char *uname;
+ size_t uname_len;
+ size_t i;
+
+ chaz_OS.shell_type = CHAZ_OS_POSIX;
+
+ /* Detect Unix name. */
+ uname = chaz_OS_run_and_capture("uname", &uname_len);
+ for (i = 0; i < CHAZ_OS_NAME_MAX && i < uname_len; i++) {
+ char c = uname[i];
+ if (!c || isspace(c)) { break; }
+ chaz_OS.name[i] = tolower(c);
+ }
+ if (i > 0) { chaz_OS.name[i] = '\0'; }
+ else { strcpy(chaz_OS.name, "unknown_unix"); }
+ free(uname);
+
strcpy(chaz_OS.dev_null, "/dev/null");
strcpy(chaz_OS.exe_ext, "");
strcpy(chaz_OS.obj_ext, ".o");
+ if (memcmp(chaz_OS.name, "darwin", 6) == 0) {
+ strcpy(chaz_OS.shared_obj_ext, ".dylib");
+ }
+ else if (memcmp(chaz_OS.name, "cygwin", 6) == 0) {
+ strcpy(chaz_OS.shared_obj_ext, ".dll");
+ }
+ else {
+ strcpy(chaz_OS.shared_obj_ext, ".so");
+ }
strcpy(chaz_OS.local_command_start, "./");
- chaz_OS.shell_type = CHAZ_OS_POSIX;
}
else if (chaz_Util_can_open_file("nul")) {
+ strcpy(chaz_OS.name, "windows");
strcpy(chaz_OS.dev_null, "nul");
strcpy(chaz_OS.exe_ext, ".exe");
strcpy(chaz_OS.obj_ext, ".obj");
+ strcpy(chaz_OS.shared_obj_ext, ".dll");
strcpy(chaz_OS.local_command_start, ".\\");
chaz_OS.shell_type = CHAZ_OS_CMD_EXE;
}
@@ -2329,6 +2892,21 @@ chaz_OS_init(void) {
}
const char*
+chaz_OS_name(void) {
+ return chaz_OS.name;
+}
+
+int
+chaz_OS_is_darwin(void) {
+ return memcmp(chaz_OS.name, "darwin", 6) == 0;
+}
+
+int
+chaz_OS_is_cygwin(void) {
+ return memcmp(chaz_OS.name, "cygwin", 6) == 0;
+}
+
+const char*
chaz_OS_exe_ext(void) {
return chaz_OS.exe_ext;
}
@@ -2339,11 +2917,21 @@ chaz_OS_obj_ext(void) {
}
const char*
+chaz_OS_shared_obj_ext(void) {
+ return chaz_OS.shared_obj_ext;
+}
+
+const char*
chaz_OS_dev_null(void) {
return chaz_OS.dev_null;
}
int
+chaz_OS_shell_type(void) {
+ return chaz_OS.shell_type;
+}
+
+int
chaz_OS_remove(const char *name) {
/*
* On Windows it can happen that another process, typically a
@@ -2417,6 +3005,15 @@ chaz_OS_run_redirected(const char *command, const char *path) {
return retval;
}
+char*
+chaz_OS_run_and_capture(const char *command, size_t *output_len) {
+ char *output;
+ chaz_OS_run_redirected(command, CHAZ_OS_TARGET_PATH);
+ output = chaz_Util_slurp_file(CHAZ_OS_TARGET_PATH, output_len);
+ chaz_Util_remove_and_verify(CHAZ_OS_TARGET_PATH);
+ return output;
+}
+
void
chaz_OS_mkdir(const char *filepath) {
char *command = NULL;
@@ -3737,6 +4334,13 @@ chaz_Strings_probe_c99_snprintf(void) {
/* #include "Charmonizer/Probe.h" */
/* #include "Charmonizer/Probe/Integers.h" */
+#define DIR_SEP "/"
+
+typedef struct SourceFileContext {
+ chaz_MakeVar *common_objs;
+ chaz_MakeVar *test_cfc_objs;
+} SourceFileContext;
+
static void
S_add_compiler_flags(struct chaz_CLIArgs *args) {
if (chaz_Probe_gcc_version_num()) {
@@ -3766,6 +4370,137 @@ S_add_compiler_flags(struct chaz_CLIArgs *args) {
}
}
+static void
+S_source_file_callback(char *file, void *context) {
+ SourceFileContext *sfc = (SourceFileContext*)context;
+ size_t file_len = strlen(file);
+ size_t obj_file_size;
+ const char *pattern;
+ char *obj_file;
+
+ if (strcmp(file, "CFCParseHeader.c") == 0) { return; }
+
+ /* Strip extension */
+ if (file_len <= 2 || memcmp(file + file_len - 2, ".c", 2) != 0) {
+ chaz_Util_warn("Unexpected source file name: %s", file);
+ return;
+ }
+ file[file_len-2] = '\0';
+
+ pattern = "$(SRC_DIR)" DIR_SEP "%s$(OBJ_EXT)";
+ obj_file_size = strlen(pattern) + file_len + 10;
+ obj_file = (char*)malloc(obj_file_size);
+ sprintf(obj_file, pattern, file);
+
+ if (strlen(file) >= 7 && memcmp(file, "CFCTest", 7) == 0) {
+ chaz_MakeVar_append(sfc->test_cfc_objs, obj_file);
+ }
+ else {
+ chaz_MakeVar_append(sfc->common_objs, obj_file);
+ }
+
+ free(obj_file);
+}
+
+static void
+S_write_makefile() {
+ SourceFileContext sfc;
+
+ const char *base_dir = "..";
+ const char *lemon_dir = ".." DIR_SEP ".." DIR_SEP ".." DIR_SEP "lemon";
+ const char *exe_ext = chaz_OS_exe_ext();
+ const char *obj_ext = chaz_OS_obj_ext();
+
+ const char *parse_header_y = "$(SRC_DIR)" DIR_SEP "CFCParseHeader.y";
+ const char *parse_header_h = "$(SRC_DIR)" DIR_SEP "CFCParseHeader.h";
+ const char *parse_header_c = "$(SRC_DIR)" DIR_SEP "CFCParseHeader.c";
+
+ char *src_dir;
+ char *scratch;
+
+ chaz_MakeFile *makefile;
+ chaz_MakeVar *var;
+ chaz_MakeRule *rule;
+
+ printf("Creating Makefile...\n");
+
+ src_dir = (char*)malloc(strlen(base_dir) + 20);
+ sprintf(src_dir, "%s" DIR_SEP "src", base_dir);
+
+ makefile = chaz_MakeFile_new();
+
+ /* Variables */
+
+ chaz_MakeFile_add_var(makefile, "SRC_DIR", src_dir);
+ scratch = (char*)malloc(strlen(base_dir) + 20);
+ sprintf(scratch, "%s" DIR_SEP "include", base_dir);
+ chaz_MakeFile_add_var(makefile, "INCLUDE_DIR", scratch);
+ free(scratch);
+ chaz_MakeFile_add_var(makefile, "LEMON_DIR", lemon_dir);
+
+ chaz_MakeFile_add_var(makefile, "EXE_EXT", exe_ext);
+ chaz_MakeFile_add_var(makefile, "OBJ_EXT", obj_ext);
+
+ chaz_MakeFile_add_var(makefile, "LEMON_EXE",
+ "$(LEMON_DIR)" DIR_SEP "lemon$(EXE_EXT)");
+
+ var = chaz_MakeFile_add_var(makefile, "CFLAGS", NULL);
+ chaz_MakeVar_append(var, chaz_CC_get_cflags());
+ chaz_MakeVar_append(var, chaz_CC_get_extra_cflags());
+ chaz_MakeVar_append(var, "-I.");
+ chaz_MakeVar_append(var, "-I$(INCLUDE_DIR)");
+ chaz_MakeVar_append(var, "-I$(SRC_DIR)");
+
+ sfc.common_objs = chaz_MakeFile_add_var(makefile, "COMMON_OBJS", NULL);
+ sfc.test_cfc_objs = chaz_MakeFile_add_var(makefile, "TEST_CFC_OBJS", NULL);
+
+ chaz_Make_list_files(src_dir, "c", S_source_file_callback, &sfc);
+
+ chaz_MakeVar_append(sfc.common_objs,
+ "$(SRC_DIR)" DIR_SEP "CFCParseHeader$(OBJ_EXT)");
+ chaz_MakeVar_append(sfc.test_cfc_objs, "t" DIR_SEP "test_cfc$(OBJ_EXT)");
+
+ chaz_MakeFile_add_var(makefile, "CFC_OBJS", "cfc$(OBJ_EXT)");
+ chaz_MakeFile_add_var(makefile, "CFC_EXE", "cfc$(EXE_EXT)");
+ chaz_MakeFile_add_var(makefile, "TEST_CFC_EXE",
+ "t" DIR_SEP "test_cfc$(EXE_EXT)");
+
+ /* Rules */
+
+ chaz_MakeFile_add_rule(makefile, "all", "$(CFC_EXE)");
+
+ rule = chaz_MakeFile_add_rule(makefile, "$(LEMON_EXE)", NULL);
+ chaz_MakeRule_add_command_make(rule, "$(LEMON_DIR)", NULL);
+
+ rule = chaz_MakeFile_add_rule(makefile, parse_header_c, NULL);
+ chaz_MakeRule_add_prereq(rule, "$(LEMON_EXE)");
+ chaz_MakeRule_add_prereq(rule, parse_header_y);
+ scratch = (char*)malloc(strlen(parse_header_y) + 20);
+ sprintf(scratch, "$(LEMON_EXE) -q %s", parse_header_y);
+ chaz_MakeRule_add_command(rule, scratch);
+ free(scratch);
+
+ chaz_MakeFile_add_rule(makefile, "$(COMMON_OBJS)", parse_header_c);
+
+ chaz_MakeFile_add_exe(makefile, "$(CFC_EXE)",
+ "$(COMMON_OBJS) $(CFC_OBJS)");
+ chaz_MakeFile_add_exe(makefile, "$(TEST_CFC_EXE)",
+ "$(COMMON_OBJS) $(TEST_CFC_OBJS)");
+
+ rule = chaz_MakeFile_add_rule(makefile, "test", "$(TEST_CFC_EXE)");
+ chaz_MakeRule_add_command(rule, "$(TEST_CFC_EXE)");
+
+ chaz_MakeFile_add_to_cleanup(makefile, "$(COMMON_OBJS)");
+ chaz_MakeFile_add_to_cleanup(makefile, "$(CFC_OBJS)");
+ chaz_MakeFile_add_to_cleanup(makefile, "$(TEST_CFC_OBJS)");
+ chaz_MakeFile_add_to_cleanup(makefile, parse_header_h);
+ chaz_MakeFile_add_to_cleanup(makefile, parse_header_c);
+
+ chaz_MakeFile_write(makefile);
+
+ free(src_dir);
+}
+
int main(int argc, const char **argv) {
/* Initialize. */
{
@@ -3785,6 +4520,16 @@ int main(int argc, const char **argv) {
chaz_Integers_run();
chaz_Strings_run();
+ {
+ int i;
+ for (i = 0; i < argc; i++) {
+ if (strncmp(argv[i], "--enable-makefile", 17) == 0) {
+ S_write_makefile();
+ break;
+ }
+ }
+ }
+
/* Clean up. */
chaz_Probe_clean_up();
http://git-wip-us.apache.org/repos/asf/lucy/blob/4cbd991e/clownfish/runtime/common/charmonizer.c
----------------------------------------------------------------------
diff --git a/clownfish/runtime/common/charmonizer.c b/clownfish/runtime/common/charmonizer.c
index fa9db46..fcb9609 100644
--- a/clownfish/runtime/common/charmonizer.c
+++ b/clownfish/runtime/common/charmonizer.c
@@ -117,6 +117,18 @@ chaz_CC_gcc_version();
int
chaz_CC_compiler_is_msvc(void);
+const char*
+chaz_CC_link_command();
+
+const char*
+chaz_CC_link_flags();
+
+const char*
+chaz_CC_link_shared_obj_flag();
+
+const char*
+chaz_CC_link_output_flag();
+
#endif /* H_CHAZ_COMPILER */
@@ -325,6 +337,70 @@ chaz_HeadCheck_contains_member(const char *struct_name, const char *member,
/***************************************************************************/
+#line 21 "src/Charmonizer/Core/Make.h"
+/* Charmonizer/Core/Compiler.h
+ */
+
+#ifndef H_CHAZ_MAKE
+#define H_CHAZ_MAKE
+
+typedef struct chaz_MakeFile chaz_MakeFile;
+typedef struct chaz_MakeVar chaz_MakeVar;
+typedef struct chaz_MakeRule chaz_MakeRule;
+
+typedef void (*chaz_Make_list_files_callback_t)(char *file, void *context);
+
+void
+chaz_Make_list_files(const char *dir, const char *ext,
+ chaz_Make_list_files_callback_t callback, void *context);
+
+chaz_MakeFile*
+chaz_MakeFile_new();
+
+chaz_MakeVar*
+chaz_MakeFile_add_var(chaz_MakeFile *makefile, const char *name,
+ const char *value);
+
+chaz_MakeRule*
+chaz_MakeFile_add_rule(chaz_MakeFile *makefile, const char *target,
+ const char *prereq);
+
+void
+chaz_MakeFile_add_to_cleanup(chaz_MakeFile *makefile, const char *target);
+
+void
+chaz_MakeFile_add_exe(chaz_MakeFile *makefile, const char *exe,
+ const char *objects);
+
+void
+chaz_MakeFile_add_shared_obj(chaz_MakeFile *makefile, const char *shared_obj,
+ const char *objects);
+
+void
+chaz_MakeFile_write(chaz_MakeFile *makefile);
+
+void
+chaz_MakeVar_append(chaz_MakeVar *var, const char *element);
+
+void
+chaz_MakeRule_add_target(chaz_MakeRule *rule, const char *target);
+
+void
+chaz_MakeRule_add_prereq(chaz_MakeRule *rule, const char *prereq);
+
+void
+chaz_MakeRule_add_command(chaz_MakeRule *rule, const char *command);
+
+void
+chaz_MakeRule_add_command_make(chaz_MakeRule *rule, const char *dir,
+ const char *target);
+
+#endif /* H_CHAZ_MAKE */
+
+
+
+/***************************************************************************/
+
#line 21 "src/Charmonizer/Core/OperatingSystem.h"
/* Charmonizer/Core/OperatingSystem.h - abstract an operating system down to a few
* variables.
@@ -333,6 +409,9 @@ chaz_HeadCheck_contains_member(const char *struct_name, const char *member,
#ifndef H_CHAZ_OPER_SYS
#define H_CHAZ_OPER_SYS
+#define CHAZ_OS_POSIX 1
+#define CHAZ_OS_CMD_EXE 2
+
/* Safely remove a file named [name]. Needed because of Windows quirks.
* Returns true on success, false on failure.
*/
@@ -358,6 +437,11 @@ chaz_OS_run_redirected(const char *command, const char *path);
int
chaz_OS_run_local_redirected(const char *command, const char *path);
+/* Run a command and return the output from stdout.
+ */
+char*
+chaz_OS_run_and_capture(const char *command, size_t *output_len);
+
/* Attempt to create a directory.
*/
void
@@ -368,6 +452,17 @@ chaz_OS_mkdir(const char *filepath);
void
chaz_OS_rmdir(const char *filepath);
+/* Return the operating system name.
+ */
+const char*
+chaz_OS_name(void);
+
+int
+chaz_OS_is_darwin(void);
+
+int
+chaz_OS_is_cygwin(void);
+
/* Return the extension for an executable on this system.
*/
const char*
@@ -378,11 +473,21 @@ chaz_OS_exe_ext(void);
const char*
chaz_OS_obj_ext(void);
+/* Return the extension for a shared object on this system.
+ */
+const char*
+chaz_OS_shared_obj_ext(void);
+
/* Return the equivalent of /dev/null on this system.
*/
const char*
chaz_OS_dev_null(void);
+/* Return the shell type of this system.
+ */
+int
+chaz_OS_shell_type(void);
+
/* Initialize the Charmonizer/Core/OperatingSystem module.
*/
void
@@ -1454,6 +1559,45 @@ chaz_CC_compiler_is_msvc(void) {
return !!chaz_CC.intval__MSC_VER;
}
+const char*
+chaz_CC_link_command() {
+ if (chaz_CC.intval__MSC_VER) {
+ return "link";
+ }
+ else {
+ return chaz_CC.cc_command;
+ }
+}
+
+const char*
+chaz_CC_link_flags() {
+ return "";
+}
+
+const char*
+chaz_CC_link_shared_obj_flag() {
+ if (chaz_CC.intval__MSC_VER) {
+ return "/DLL";
+ }
+ else if (chaz_OS_is_darwin()) {
+ return "-dynamiclib ";
+ }
+ else {
+ return "-shared";
+ }
+}
+
+const char*
+chaz_CC_link_output_flag() {
+ if (chaz_CC.intval__MSC_VER) {
+ return "/OUT:";
+ }
+ else {
+ return "-o ";
+ }
+}
+
+
/***************************************************************************/
@@ -2559,25 +2703,417 @@ chaz_HeadCheck_maybe_add_to_cache(const char *header_name, int exists) {
/***************************************************************************/
+#line 17 "src/Charmonizer/Core/Make.c"
+#include <string.h>
+/* #include "Charmonizer/Core/Make.h" */
+/* #include "Charmonizer/Core/Compiler.h" */
+/* #include "Charmonizer/Core/OperatingSystem.h" */
+/* #include "Charmonizer/Core/Util.h" */
+
+struct chaz_MakeVar {
+ char *name;
+ char *value;
+ size_t num_elements;
+};
+
+struct chaz_MakeRule {
+ char *targets;
+ char *prereqs;
+ char *commands;
+};
+
+struct chaz_MakeFile {
+ chaz_MakeVar **vars;
+ size_t num_vars;
+ chaz_MakeRule **rules;
+ size_t num_rules;
+ char **cleanups;
+ size_t num_cleanups;
+};
+
+chaz_MakeFile*
+chaz_MakeFile_new() {
+ chaz_MakeFile *makefile = (chaz_MakeFile*)malloc(sizeof(chaz_MakeFile));
+
+ makefile->vars = (chaz_MakeVar**)malloc(sizeof(chaz_MakeVar*));
+ makefile->vars[0] = NULL;
+ makefile->num_vars = 0;
+
+ makefile->rules = (chaz_MakeRule**)malloc(sizeof(chaz_MakeRule*));
+ makefile->rules[0] = NULL;
+ makefile->num_rules = 0;
+
+ makefile->cleanups = (char**)malloc(sizeof(char*));
+ makefile->cleanups[0] = NULL;
+ makefile->num_cleanups = 0;
+
+ return makefile;
+}
+
+chaz_MakeVar*
+chaz_MakeFile_add_var(chaz_MakeFile *makefile, const char *name,
+ const char *value) {
+ chaz_MakeVar *var = (chaz_MakeVar*)malloc(sizeof(chaz_MakeVar));
+ chaz_MakeVar **vars = makefile->vars;
+ size_t num_vars = makefile->num_vars + 1;
+
+ var->name = chaz_Util_strdup(name);
+ var->value = chaz_Util_strdup("");
+ var->num_elements = 0;
+
+ if (value) { chaz_MakeVar_append(var, value); }
+
+ vars = (chaz_MakeVar**)realloc(vars,
+ (num_vars + 1) * sizeof(chaz_MakeVar*));
+ vars[num_vars-1] = var;
+ vars[num_vars] = NULL;
+ makefile->vars = vars;
+ makefile->num_vars = num_vars;
+
+ return var;
+}
+
+chaz_MakeRule*
+chaz_MakeFile_add_rule(chaz_MakeFile *makefile, const char *target,
+ const char *prereq) {
+ chaz_MakeRule *rule = (chaz_MakeRule*)malloc(sizeof(chaz_MakeRule));
+ chaz_MakeRule **rules = makefile->rules;
+ size_t num_rules = makefile->num_rules + 1;
+
+ rule->targets = NULL;
+ rule->prereqs = NULL;
+ rule->commands = NULL;
+
+ if (target) { chaz_MakeRule_add_target(rule, target); }
+ if (prereq) { chaz_MakeRule_add_prereq(rule, prereq); }
+
+ rules = (chaz_MakeRule**)realloc(rules,
+ (num_rules + 1) * sizeof(chaz_MakeRule*));
+ rules[num_rules-1] = rule;
+ rules[num_rules] = NULL;
+ makefile->rules = rules;
+ makefile->num_rules = num_rules;
+
+ return rule;
+}
+
+void
+chaz_MakeFile_add_to_cleanup(chaz_MakeFile *makefile, const char *target) {
+ char *cleanup = chaz_Util_strdup(target);
+ char **cleanups = makefile->cleanups;
+ size_t num_cleanups = makefile->num_cleanups + 1;
+
+ cleanups = (char**)realloc(cleanups, (num_cleanups + 1) * sizeof(char*));
+ cleanups[num_cleanups-1] = cleanup;
+ cleanups[num_cleanups] = NULL;
+ makefile->cleanups = cleanups;
+ makefile->num_cleanups = num_cleanups;
+}
+
+void
+chaz_MakeFile_add_exe(chaz_MakeFile *makefile, const char *exe,
+ const char *objects) {
+ const char *pattern = "%s %s %s %s%s";
+ const char *link = chaz_CC_link_command();
+ const char *link_flags = chaz_CC_link_flags();
+ const char *output_flag = chaz_CC_link_output_flag();
+ chaz_MakeRule *rule;
+ char *command;
+ size_t size;
+
+ rule = chaz_MakeFile_add_rule(makefile, exe, objects);
+
+ size = strlen(pattern)
+ + strlen(link)
+ + strlen(link_flags)
+ + strlen(objects)
+ + strlen(output_flag)
+ + strlen(exe)
+ + 50;
+ command = (char*)malloc(size);
+ sprintf(command, pattern, link, link_flags, objects, output_flag, exe);
+ chaz_MakeRule_add_command(rule, command);
+
+ chaz_MakeFile_add_to_cleanup(makefile, exe);
+}
+
+void
+chaz_MakeFile_add_shared_obj(chaz_MakeFile *makefile, const char *shared_obj,
+ const char *objects) {
+ const char *pattern = "%s %s %s %s %s%s";
+ const char *link = chaz_CC_link_command();
+ const char *shobj_flags = chaz_CC_link_shared_obj_flag();
+ const char *link_flags = chaz_CC_link_flags();
+ const char *output_flag = chaz_CC_link_output_flag();
+ chaz_MakeRule *rule;
+ char *command;
+ size_t size;
+
+ rule = chaz_MakeFile_add_rule(makefile, shared_obj, objects);
+
+ size = strlen(pattern)
+ + strlen(link)
+ + strlen(shobj_flags)
+ + strlen(link_flags)
+ + strlen(objects)
+ + strlen(output_flag)
+ + strlen(shared_obj)
+ + 50;
+ command = (char*)malloc(size);
+ sprintf(command, pattern, link, shobj_flags, link_flags, objects,
+ output_flag, shared_obj);
+ chaz_MakeRule_add_command(rule, command);
+
+ chaz_MakeFile_add_to_cleanup(makefile, shared_obj);
+}
+
+void
+chaz_MakeFile_write(chaz_MakeFile *makefile) {
+ FILE *file;
+ size_t i;
+
+ file = fopen("Makefile", "w");
+ if (!file) {
+ chaz_Util_die("Can't open Makefile\n");
+ }
+
+ for (i = 0; makefile->vars[i]; i++) {
+ chaz_MakeVar *var = makefile->vars[i];
+ fprintf(file, "%s = %s\n", var->name, var->value);
+ }
+ fprintf(file, "\n");
+
+ for (i = 0; makefile->rules[i]; i++) {
+ chaz_MakeRule *rule = makefile->rules[i];
+ fprintf(file, "%s :", rule->targets);
+ if (rule->prereqs) {
+ fprintf(file, " %s", rule->prereqs);
+ }
+ fprintf(file, "\n");
+ if (rule->commands) {
+ fprintf(file, "%s", rule->commands);
+ }
+ fprintf(file, "\n");
+ }
+
+ if (makefile->cleanups[0]) {
+ fprintf(file, "clean :\n\trm -f");
+ for (i = 0; makefile->cleanups[i]; i++) {
+ char *cleanup = makefile->cleanups[i];
+ fprintf(file, " \\\n\t %s", cleanup);
+ }
+ fprintf(file, "\n\n");
+ }
+
+ fprintf(file, "distclean : clean\n");
+ fprintf(file, "\trm -f charmonizer charmony.h Makefile\n\n");
+
+ fclose(file);
+}
+
+void
+chaz_MakeVar_append(chaz_MakeVar *var, const char *element) {
+ char *value;
+
+ if (element[0] == '\0') { return; }
+
+ if (var->num_elements == 0) {
+ value = chaz_Util_strdup(element);
+ }
+ else {
+ value = (char*)malloc(strlen(var->value) + strlen(element) + 20);
+
+ if (var->num_elements == 1) {
+ sprintf(value, "\\\n %s \\\n %s", var->value, element);
+ }
+ else {
+ sprintf(value, "%s \\\n %s", var->value, element);
+ }
+ }
+
+ free(var->value);
+ var->value = value;
+ var->num_elements++;
+}
+
+void
+chaz_MakeRule_add_target(chaz_MakeRule *rule, const char *target) {
+ char *targets;
+
+ if (!rule->targets) {
+ targets = chaz_Util_strdup(target);
+ }
+ else {
+ targets = (char*)malloc(strlen(rule->targets) + strlen(target) + 20);
+ sprintf(targets, "%s %s", rule->targets, target);
+ free(rule->targets);
+ }
+
+ rule->targets = targets;
+}
+
+void
+chaz_MakeRule_add_prereq(chaz_MakeRule *rule, const char *prereq) {
+ char *prereqs;
+
+ if (!rule->prereqs) {
+ prereqs = chaz_Util_strdup(prereq);
+ }
+ else {
+ prereqs = (char*)malloc(strlen(rule->prereqs) + strlen(prereq) + 20);
+ sprintf(prereqs, "%s %s", rule->prereqs, prereq);
+ free(rule->prereqs);
+ }
+
+ rule->prereqs = prereqs;
+}
+
+void
+chaz_MakeRule_add_command(chaz_MakeRule *rule, const char *command) {
+ char *commands;
+
+ if (!rule->commands) {
+ commands = (char*)malloc(strlen(command) + 20);
+ sprintf(commands, "\t%s\n", command);
+ }
+ else {
+ commands = (char*)malloc(strlen(rule->commands) + strlen(command) + 20);
+ sprintf(commands, "%s\t%s\n", rule->commands, command);
+ free(rule->commands);
+ }
+
+ rule->commands = commands;
+}
+
+void
+chaz_MakeRule_add_command_make(chaz_MakeRule *rule, const char *dir,
+ const char *target) {
+ char *command;
+
+ if (!target) {
+ command = (char*)malloc(strlen(dir) + 20);
+ sprintf(command, "make -C %s", dir);
+ }
+ else {
+ command = (char*)malloc(strlen(dir) + strlen(target) + 20);
+ sprintf(command, "make -C %s %s", dir, target);
+ }
+
+ chaz_MakeRule_add_command(rule, command);
+
+ free(command);
+}
+
+void
+chaz_Make_list_files(const char *dir, const char *ext,
+ chaz_Make_list_files_callback_t callback, void *context) {
+ int shell_type = chaz_OS_shell_type();
+ const char *pattern;
+ char *command;
+ char *list;
+ char *prefix;
+ char *file;
+ size_t command_size;
+ size_t list_len;
+ size_t prefix_len;
+
+ /* List files using shell. */
+
+ if (shell_type == CHAZ_OS_POSIX) {
+ pattern = "find %s -name '*.%s' -type f";
+ }
+ else if (shell_type == CHAZ_OS_CMD_EXE) {
+ pattern = "dir %s\\*.%s /s /b /a-d";
+ }
+ else {
+ chaz_Util_die("Unknown shell type %d", shell_type);
+ }
+
+ command_size = strlen(pattern) + strlen(dir) + strlen(ext) + 10;
+ command = (char*)malloc(command_size);
+ sprintf(command, pattern, dir, ext);
+ list = chaz_OS_run_and_capture(command, &list_len);
+ free(command);
+ if (!list) {
+ chaz_Util_die("Failed to list files in '%s'", dir);
+ }
+ list[list_len-1] = 0;
+
+ /* Find directory prefix to strip from files */
+
+ if (shell_type == CHAZ_OS_POSIX) {
+ prefix_len = strlen(dir);
+ prefix = (char*)malloc(prefix_len + 2);
+ memcpy(prefix, dir, prefix_len);
+ prefix[prefix_len++] = '/';
+ prefix[prefix_len] = '\0';
+ }
+ else {
+ char *output;
+ size_t output_len;
+
+ /* 'dir /s' returns absolute paths, so we have to find the absolute
+ * path of the directory. This is done by using the variable
+ * substitution feature of the 'for' command.
+ */
+ pattern = "for %%I in (%s) do echo %%~fI";
+ command_size = strlen(pattern) + strlen(dir) + 10;
+ command = (char*)malloc(command_size);
+ sprintf(command, pattern, dir);
+ output = chaz_OS_run_and_capture(command, &output_len);
+ free(command);
+ if (!output) { chaz_Util_die("Failed to find absolute path"); }
+
+ prefix_len = strcspn(output, "\r\n");
+ prefix = (char*)malloc(prefix_len + 2);
+ memcpy(prefix, output, prefix_len);
+ prefix[prefix_len++] = '\\';
+ prefix[prefix_len] = '\0';
+ free(output);
+ }
+
+ /* Iterate file list and invoke callback. */
+
+ for (file = strtok(list, "\r\n"); file; file = strtok(NULL, "\r\n")) {
+ if (strlen(file) <= prefix_len
+ || memcmp(file, prefix, prefix_len) != 0
+ ) {
+ chaz_Util_die("Unexpected file name");
+ }
+
+ callback(file + prefix_len, context);
+ }
+
+ free(prefix);
+ free(list);
+}
+
+
+/***************************************************************************/
+
#line 17 "src/Charmonizer/Core/OperatingSystem.c"
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
+#include <ctype.h>
/* #include "Charmonizer/Core/Compiler.h" */
/* #include "Charmonizer/Core/Util.h" */
/* #include "Charmonizer/Core/ConfWriter.h" */
/* #include "Charmonizer/Core/OperatingSystem.h" */
+#define CHAZ_OS_TARGET_PATH "_charmonizer_target"
+#define CHAZ_OS_NAME_MAX 31
+
static struct {
+ char name[CHAZ_OS_NAME_MAX+1];
char dev_null[20];
char exe_ext[5];
char obj_ext[5];
+ char shared_obj_ext[7];
char local_command_start[3];
int shell_type;
-} chaz_OS = { "", "", "", "", 0 };
-#define CHAZ_OS_POSIX 1
-#define CHAZ_OS_CMD_EXE 2
+} chaz_OS = { "", "", "", "", "", "", 0 };
void
chaz_OS_init(void) {
@@ -2591,16 +3127,43 @@ chaz_OS_init(void) {
/* Detect shell based on whether the bitbucket is "/dev/null" or "nul". */
if (chaz_Util_can_open_file("/dev/null")) {
+ char *uname;
+ size_t uname_len;
+ size_t i;
+
+ chaz_OS.shell_type = CHAZ_OS_POSIX;
+
+ /* Detect Unix name. */
+ uname = chaz_OS_run_and_capture("uname", &uname_len);
+ for (i = 0; i < CHAZ_OS_NAME_MAX && i < uname_len; i++) {
+ char c = uname[i];
+ if (!c || isspace(c)) { break; }
+ chaz_OS.name[i] = tolower(c);
+ }
+ if (i > 0) { chaz_OS.name[i] = '\0'; }
+ else { strcpy(chaz_OS.name, "unknown_unix"); }
+ free(uname);
+
strcpy(chaz_OS.dev_null, "/dev/null");
strcpy(chaz_OS.exe_ext, "");
strcpy(chaz_OS.obj_ext, ".o");
+ if (memcmp(chaz_OS.name, "darwin", 6) == 0) {
+ strcpy(chaz_OS.shared_obj_ext, ".dylib");
+ }
+ else if (memcmp(chaz_OS.name, "cygwin", 6) == 0) {
+ strcpy(chaz_OS.shared_obj_ext, ".dll");
+ }
+ else {
+ strcpy(chaz_OS.shared_obj_ext, ".so");
+ }
strcpy(chaz_OS.local_command_start, "./");
- chaz_OS.shell_type = CHAZ_OS_POSIX;
}
else if (chaz_Util_can_open_file("nul")) {
+ strcpy(chaz_OS.name, "windows");
strcpy(chaz_OS.dev_null, "nul");
strcpy(chaz_OS.exe_ext, ".exe");
strcpy(chaz_OS.obj_ext, ".obj");
+ strcpy(chaz_OS.shared_obj_ext, ".dll");
strcpy(chaz_OS.local_command_start, ".\\");
chaz_OS.shell_type = CHAZ_OS_CMD_EXE;
}
@@ -2611,6 +3174,21 @@ chaz_OS_init(void) {
}
const char*
+chaz_OS_name(void) {
+ return chaz_OS.name;
+}
+
+int
+chaz_OS_is_darwin(void) {
+ return memcmp(chaz_OS.name, "darwin", 6) == 0;
+}
+
+int
+chaz_OS_is_cygwin(void) {
+ return memcmp(chaz_OS.name, "cygwin", 6) == 0;
+}
+
+const char*
chaz_OS_exe_ext(void) {
return chaz_OS.exe_ext;
}
@@ -2621,11 +3199,21 @@ chaz_OS_obj_ext(void) {
}
const char*
+chaz_OS_shared_obj_ext(void) {
+ return chaz_OS.shared_obj_ext;
+}
+
+const char*
chaz_OS_dev_null(void) {
return chaz_OS.dev_null;
}
int
+chaz_OS_shell_type(void) {
+ return chaz_OS.shell_type;
+}
+
+int
chaz_OS_remove(const char *name) {
/*
* On Windows it can happen that another process, typically a
@@ -2699,6 +3287,15 @@ chaz_OS_run_redirected(const char *command, const char *path) {
return retval;
}
+char*
+chaz_OS_run_and_capture(const char *command, size_t *output_len) {
+ char *output;
+ chaz_OS_run_redirected(command, CHAZ_OS_TARGET_PATH);
+ output = chaz_Util_slurp_file(CHAZ_OS_TARGET_PATH, output_len);
+ chaz_Util_remove_and_verify(CHAZ_OS_TARGET_PATH);
+ return output;
+}
+
void
chaz_OS_mkdir(const char *filepath) {
char *command = NULL;
http://git-wip-us.apache.org/repos/asf/lucy/blob/4cbd991e/common/charmonizer.c
----------------------------------------------------------------------
diff --git a/common/charmonizer.c b/common/charmonizer.c
index 0e22822..8a4feeb 100644
--- a/common/charmonizer.c
+++ b/common/charmonizer.c
@@ -117,6 +117,18 @@ chaz_CC_gcc_version();
int
chaz_CC_compiler_is_msvc(void);
+const char*
+chaz_CC_link_command();
+
+const char*
+chaz_CC_link_flags();
+
+const char*
+chaz_CC_link_shared_obj_flag();
+
+const char*
+chaz_CC_link_output_flag();
+
#endif /* H_CHAZ_COMPILER */
@@ -325,6 +337,70 @@ chaz_HeadCheck_contains_member(const char *struct_name, const char *member,
/***************************************************************************/
+#line 21 "src/Charmonizer/Core/Make.h"
+/* Charmonizer/Core/Compiler.h
+ */
+
+#ifndef H_CHAZ_MAKE
+#define H_CHAZ_MAKE
+
+typedef struct chaz_MakeFile chaz_MakeFile;
+typedef struct chaz_MakeVar chaz_MakeVar;
+typedef struct chaz_MakeRule chaz_MakeRule;
+
+typedef void (*chaz_Make_list_files_callback_t)(char *file, void *context);
+
+void
+chaz_Make_list_files(const char *dir, const char *ext,
+ chaz_Make_list_files_callback_t callback, void *context);
+
+chaz_MakeFile*
+chaz_MakeFile_new();
+
+chaz_MakeVar*
+chaz_MakeFile_add_var(chaz_MakeFile *makefile, const char *name,
+ const char *value);
+
+chaz_MakeRule*
+chaz_MakeFile_add_rule(chaz_MakeFile *makefile, const char *target,
+ const char *prereq);
+
+void
+chaz_MakeFile_add_to_cleanup(chaz_MakeFile *makefile, const char *target);
+
+void
+chaz_MakeFile_add_exe(chaz_MakeFile *makefile, const char *exe,
+ const char *objects);
+
+void
+chaz_MakeFile_add_shared_obj(chaz_MakeFile *makefile, const char *shared_obj,
+ const char *objects);
+
+void
+chaz_MakeFile_write(chaz_MakeFile *makefile);
+
+void
+chaz_MakeVar_append(chaz_MakeVar *var, const char *element);
+
+void
+chaz_MakeRule_add_target(chaz_MakeRule *rule, const char *target);
+
+void
+chaz_MakeRule_add_prereq(chaz_MakeRule *rule, const char *prereq);
+
+void
+chaz_MakeRule_add_command(chaz_MakeRule *rule, const char *command);
+
+void
+chaz_MakeRule_add_command_make(chaz_MakeRule *rule, const char *dir,
+ const char *target);
+
+#endif /* H_CHAZ_MAKE */
+
+
+
+/***************************************************************************/
+
#line 21 "src/Charmonizer/Core/OperatingSystem.h"
/* Charmonizer/Core/OperatingSystem.h - abstract an operating system down to a few
* variables.
@@ -333,6 +409,9 @@ chaz_HeadCheck_contains_member(const char *struct_name, const char *member,
#ifndef H_CHAZ_OPER_SYS
#define H_CHAZ_OPER_SYS
+#define CHAZ_OS_POSIX 1
+#define CHAZ_OS_CMD_EXE 2
+
/* Safely remove a file named [name]. Needed because of Windows quirks.
* Returns true on success, false on failure.
*/
@@ -358,6 +437,11 @@ chaz_OS_run_redirected(const char *command, const char *path);
int
chaz_OS_run_local_redirected(const char *command, const char *path);
+/* Run a command and return the output from stdout.
+ */
+char*
+chaz_OS_run_and_capture(const char *command, size_t *output_len);
+
/* Attempt to create a directory.
*/
void
@@ -368,6 +452,17 @@ chaz_OS_mkdir(const char *filepath);
void
chaz_OS_rmdir(const char *filepath);
+/* Return the operating system name.
+ */
+const char*
+chaz_OS_name(void);
+
+int
+chaz_OS_is_darwin(void);
+
+int
+chaz_OS_is_cygwin(void);
+
/* Return the extension for an executable on this system.
*/
const char*
@@ -378,11 +473,21 @@ chaz_OS_exe_ext(void);
const char*
chaz_OS_obj_ext(void);
+/* Return the extension for a shared object on this system.
+ */
+const char*
+chaz_OS_shared_obj_ext(void);
+
/* Return the equivalent of /dev/null on this system.
*/
const char*
chaz_OS_dev_null(void);
+/* Return the shell type of this system.
+ */
+int
+chaz_OS_shell_type(void);
+
/* Initialize the Charmonizer/Core/OperatingSystem module.
*/
void
@@ -1454,6 +1559,45 @@ chaz_CC_compiler_is_msvc(void) {
return !!chaz_CC.intval__MSC_VER;
}
+const char*
+chaz_CC_link_command() {
+ if (chaz_CC.intval__MSC_VER) {
+ return "link";
+ }
+ else {
+ return chaz_CC.cc_command;
+ }
+}
+
+const char*
+chaz_CC_link_flags() {
+ return "";
+}
+
+const char*
+chaz_CC_link_shared_obj_flag() {
+ if (chaz_CC.intval__MSC_VER) {
+ return "/DLL";
+ }
+ else if (chaz_OS_is_darwin()) {
+ return "-dynamiclib ";
+ }
+ else {
+ return "-shared";
+ }
+}
+
+const char*
+chaz_CC_link_output_flag() {
+ if (chaz_CC.intval__MSC_VER) {
+ return "/OUT:";
+ }
+ else {
+ return "-o ";
+ }
+}
+
+
/***************************************************************************/
@@ -2559,25 +2703,417 @@ chaz_HeadCheck_maybe_add_to_cache(const char *header_name, int exists) {
/***************************************************************************/
+#line 17 "src/Charmonizer/Core/Make.c"
+#include <string.h>
+/* #include "Charmonizer/Core/Make.h" */
+/* #include "Charmonizer/Core/Compiler.h" */
+/* #include "Charmonizer/Core/OperatingSystem.h" */
+/* #include "Charmonizer/Core/Util.h" */
+
+struct chaz_MakeVar {
+ char *name;
+ char *value;
+ size_t num_elements;
+};
+
+struct chaz_MakeRule {
+ char *targets;
+ char *prereqs;
+ char *commands;
+};
+
+struct chaz_MakeFile {
+ chaz_MakeVar **vars;
+ size_t num_vars;
+ chaz_MakeRule **rules;
+ size_t num_rules;
+ char **cleanups;
+ size_t num_cleanups;
+};
+
+chaz_MakeFile*
+chaz_MakeFile_new() {
+ chaz_MakeFile *makefile = (chaz_MakeFile*)malloc(sizeof(chaz_MakeFile));
+
+ makefile->vars = (chaz_MakeVar**)malloc(sizeof(chaz_MakeVar*));
+ makefile->vars[0] = NULL;
+ makefile->num_vars = 0;
+
+ makefile->rules = (chaz_MakeRule**)malloc(sizeof(chaz_MakeRule*));
+ makefile->rules[0] = NULL;
+ makefile->num_rules = 0;
+
+ makefile->cleanups = (char**)malloc(sizeof(char*));
+ makefile->cleanups[0] = NULL;
+ makefile->num_cleanups = 0;
+
+ return makefile;
+}
+
+chaz_MakeVar*
+chaz_MakeFile_add_var(chaz_MakeFile *makefile, const char *name,
+ const char *value) {
+ chaz_MakeVar *var = (chaz_MakeVar*)malloc(sizeof(chaz_MakeVar));
+ chaz_MakeVar **vars = makefile->vars;
+ size_t num_vars = makefile->num_vars + 1;
+
+ var->name = chaz_Util_strdup(name);
+ var->value = chaz_Util_strdup("");
+ var->num_elements = 0;
+
+ if (value) { chaz_MakeVar_append(var, value); }
+
+ vars = (chaz_MakeVar**)realloc(vars,
+ (num_vars + 1) * sizeof(chaz_MakeVar*));
+ vars[num_vars-1] = var;
+ vars[num_vars] = NULL;
+ makefile->vars = vars;
+ makefile->num_vars = num_vars;
+
+ return var;
+}
+
+chaz_MakeRule*
+chaz_MakeFile_add_rule(chaz_MakeFile *makefile, const char *target,
+ const char *prereq) {
+ chaz_MakeRule *rule = (chaz_MakeRule*)malloc(sizeof(chaz_MakeRule));
+ chaz_MakeRule **rules = makefile->rules;
+ size_t num_rules = makefile->num_rules + 1;
+
+ rule->targets = NULL;
+ rule->prereqs = NULL;
+ rule->commands = NULL;
+
+ if (target) { chaz_MakeRule_add_target(rule, target); }
+ if (prereq) { chaz_MakeRule_add_prereq(rule, prereq); }
+
+ rules = (chaz_MakeRule**)realloc(rules,
+ (num_rules + 1) * sizeof(chaz_MakeRule*));
+ rules[num_rules-1] = rule;
+ rules[num_rules] = NULL;
+ makefile->rules = rules;
+ makefile->num_rules = num_rules;
+
+ return rule;
+}
+
+void
+chaz_MakeFile_add_to_cleanup(chaz_MakeFile *makefile, const char *target) {
+ char *cleanup = chaz_Util_strdup(target);
+ char **cleanups = makefile->cleanups;
+ size_t num_cleanups = makefile->num_cleanups + 1;
+
+ cleanups = (char**)realloc(cleanups, (num_cleanups + 1) * sizeof(char*));
+ cleanups[num_cleanups-1] = cleanup;
+ cleanups[num_cleanups] = NULL;
+ makefile->cleanups = cleanups;
+ makefile->num_cleanups = num_cleanups;
+}
+
+void
+chaz_MakeFile_add_exe(chaz_MakeFile *makefile, const char *exe,
+ const char *objects) {
+ const char *pattern = "%s %s %s %s%s";
+ const char *link = chaz_CC_link_command();
+ const char *link_flags = chaz_CC_link_flags();
+ const char *output_flag = chaz_CC_link_output_flag();
+ chaz_MakeRule *rule;
+ char *command;
+ size_t size;
+
+ rule = chaz_MakeFile_add_rule(makefile, exe, objects);
+
+ size = strlen(pattern)
+ + strlen(link)
+ + strlen(link_flags)
+ + strlen(objects)
+ + strlen(output_flag)
+ + strlen(exe)
+ + 50;
+ command = (char*)malloc(size);
+ sprintf(command, pattern, link, link_flags, objects, output_flag, exe);
+ chaz_MakeRule_add_command(rule, command);
+
+ chaz_MakeFile_add_to_cleanup(makefile, exe);
+}
+
+void
+chaz_MakeFile_add_shared_obj(chaz_MakeFile *makefile, const char *shared_obj,
+ const char *objects) {
+ const char *pattern = "%s %s %s %s %s%s";
+ const char *link = chaz_CC_link_command();
+ const char *shobj_flags = chaz_CC_link_shared_obj_flag();
+ const char *link_flags = chaz_CC_link_flags();
+ const char *output_flag = chaz_CC_link_output_flag();
+ chaz_MakeRule *rule;
+ char *command;
+ size_t size;
+
+ rule = chaz_MakeFile_add_rule(makefile, shared_obj, objects);
+
+ size = strlen(pattern)
+ + strlen(link)
+ + strlen(shobj_flags)
+ + strlen(link_flags)
+ + strlen(objects)
+ + strlen(output_flag)
+ + strlen(shared_obj)
+ + 50;
+ command = (char*)malloc(size);
+ sprintf(command, pattern, link, shobj_flags, link_flags, objects,
+ output_flag, shared_obj);
+ chaz_MakeRule_add_command(rule, command);
+
+ chaz_MakeFile_add_to_cleanup(makefile, shared_obj);
+}
+
+void
+chaz_MakeFile_write(chaz_MakeFile *makefile) {
+ FILE *file;
+ size_t i;
+
+ file = fopen("Makefile", "w");
+ if (!file) {
+ chaz_Util_die("Can't open Makefile\n");
+ }
+
+ for (i = 0; makefile->vars[i]; i++) {
+ chaz_MakeVar *var = makefile->vars[i];
+ fprintf(file, "%s = %s\n", var->name, var->value);
+ }
+ fprintf(file, "\n");
+
+ for (i = 0; makefile->rules[i]; i++) {
+ chaz_MakeRule *rule = makefile->rules[i];
+ fprintf(file, "%s :", rule->targets);
+ if (rule->prereqs) {
+ fprintf(file, " %s", rule->prereqs);
+ }
+ fprintf(file, "\n");
+ if (rule->commands) {
+ fprintf(file, "%s", rule->commands);
+ }
+ fprintf(file, "\n");
+ }
+
+ if (makefile->cleanups[0]) {
+ fprintf(file, "clean :\n\trm -f");
+ for (i = 0; makefile->cleanups[i]; i++) {
+ char *cleanup = makefile->cleanups[i];
+ fprintf(file, " \\\n\t %s", cleanup);
+ }
+ fprintf(file, "\n\n");
+ }
+
+ fprintf(file, "distclean : clean\n");
+ fprintf(file, "\trm -f charmonizer charmony.h Makefile\n\n");
+
+ fclose(file);
+}
+
+void
+chaz_MakeVar_append(chaz_MakeVar *var, const char *element) {
+ char *value;
+
+ if (element[0] == '\0') { return; }
+
+ if (var->num_elements == 0) {
+ value = chaz_Util_strdup(element);
+ }
+ else {
+ value = (char*)malloc(strlen(var->value) + strlen(element) + 20);
+
+ if (var->num_elements == 1) {
+ sprintf(value, "\\\n %s \\\n %s", var->value, element);
+ }
+ else {
+ sprintf(value, "%s \\\n %s", var->value, element);
+ }
+ }
+
+ free(var->value);
+ var->value = value;
+ var->num_elements++;
+}
+
+void
+chaz_MakeRule_add_target(chaz_MakeRule *rule, const char *target) {
+ char *targets;
+
+ if (!rule->targets) {
+ targets = chaz_Util_strdup(target);
+ }
+ else {
+ targets = (char*)malloc(strlen(rule->targets) + strlen(target) + 20);
+ sprintf(targets, "%s %s", rule->targets, target);
+ free(rule->targets);
+ }
+
+ rule->targets = targets;
+}
+
+void
+chaz_MakeRule_add_prereq(chaz_MakeRule *rule, const char *prereq) {
+ char *prereqs;
+
+ if (!rule->prereqs) {
+ prereqs = chaz_Util_strdup(prereq);
+ }
+ else {
+ prereqs = (char*)malloc(strlen(rule->prereqs) + strlen(prereq) + 20);
+ sprintf(prereqs, "%s %s", rule->prereqs, prereq);
+ free(rule->prereqs);
+ }
+
+ rule->prereqs = prereqs;
+}
+
+void
+chaz_MakeRule_add_command(chaz_MakeRule *rule, const char *command) {
+ char *commands;
+
+ if (!rule->commands) {
+ commands = (char*)malloc(strlen(command) + 20);
+ sprintf(commands, "\t%s\n", command);
+ }
+ else {
+ commands = (char*)malloc(strlen(rule->commands) + strlen(command) + 20);
+ sprintf(commands, "%s\t%s\n", rule->commands, command);
+ free(rule->commands);
+ }
+
+ rule->commands = commands;
+}
+
+void
+chaz_MakeRule_add_command_make(chaz_MakeRule *rule, const char *dir,
+ const char *target) {
+ char *command;
+
+ if (!target) {
+ command = (char*)malloc(strlen(dir) + 20);
+ sprintf(command, "make -C %s", dir);
+ }
+ else {
+ command = (char*)malloc(strlen(dir) + strlen(target) + 20);
+ sprintf(command, "make -C %s %s", dir, target);
+ }
+
+ chaz_MakeRule_add_command(rule, command);
+
+ free(command);
+}
+
+void
+chaz_Make_list_files(const char *dir, const char *ext,
+ chaz_Make_list_files_callback_t callback, void *context) {
+ int shell_type = chaz_OS_shell_type();
+ const char *pattern;
+ char *command;
+ char *list;
+ char *prefix;
+ char *file;
+ size_t command_size;
+ size_t list_len;
+ size_t prefix_len;
+
+ /* List files using shell. */
+
+ if (shell_type == CHAZ_OS_POSIX) {
+ pattern = "find %s -name '*.%s' -type f";
+ }
+ else if (shell_type == CHAZ_OS_CMD_EXE) {
+ pattern = "dir %s\\*.%s /s /b /a-d";
+ }
+ else {
+ chaz_Util_die("Unknown shell type %d", shell_type);
+ }
+
+ command_size = strlen(pattern) + strlen(dir) + strlen(ext) + 10;
+ command = (char*)malloc(command_size);
+ sprintf(command, pattern, dir, ext);
+ list = chaz_OS_run_and_capture(command, &list_len);
+ free(command);
+ if (!list) {
+ chaz_Util_die("Failed to list files in '%s'", dir);
+ }
+ list[list_len-1] = 0;
+
+ /* Find directory prefix to strip from files */
+
+ if (shell_type == CHAZ_OS_POSIX) {
+ prefix_len = strlen(dir);
+ prefix = (char*)malloc(prefix_len + 2);
+ memcpy(prefix, dir, prefix_len);
+ prefix[prefix_len++] = '/';
+ prefix[prefix_len] = '\0';
+ }
+ else {
+ char *output;
+ size_t output_len;
+
+ /* 'dir /s' returns absolute paths, so we have to find the absolute
+ * path of the directory. This is done by using the variable
+ * substitution feature of the 'for' command.
+ */
+ pattern = "for %%I in (%s) do echo %%~fI";
+ command_size = strlen(pattern) + strlen(dir) + 10;
+ command = (char*)malloc(command_size);
+ sprintf(command, pattern, dir);
+ output = chaz_OS_run_and_capture(command, &output_len);
+ free(command);
+ if (!output) { chaz_Util_die("Failed to find absolute path"); }
+
+ prefix_len = strcspn(output, "\r\n");
+ prefix = (char*)malloc(prefix_len + 2);
+ memcpy(prefix, output, prefix_len);
+ prefix[prefix_len++] = '\\';
+ prefix[prefix_len] = '\0';
+ free(output);
+ }
+
+ /* Iterate file list and invoke callback. */
+
+ for (file = strtok(list, "\r\n"); file; file = strtok(NULL, "\r\n")) {
+ if (strlen(file) <= prefix_len
+ || memcmp(file, prefix, prefix_len) != 0
+ ) {
+ chaz_Util_die("Unexpected file name");
+ }
+
+ callback(file + prefix_len, context);
+ }
+
+ free(prefix);
+ free(list);
+}
+
+
+/***************************************************************************/
+
#line 17 "src/Charmonizer/Core/OperatingSystem.c"
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
+#include <ctype.h>
/* #include "Charmonizer/Core/Compiler.h" */
/* #include "Charmonizer/Core/Util.h" */
/* #include "Charmonizer/Core/ConfWriter.h" */
/* #include "Charmonizer/Core/OperatingSystem.h" */
+#define CHAZ_OS_TARGET_PATH "_charmonizer_target"
+#define CHAZ_OS_NAME_MAX 31
+
static struct {
+ char name[CHAZ_OS_NAME_MAX+1];
char dev_null[20];
char exe_ext[5];
char obj_ext[5];
+ char shared_obj_ext[7];
char local_command_start[3];
int shell_type;
-} chaz_OS = { "", "", "", "", 0 };
-#define CHAZ_OS_POSIX 1
-#define CHAZ_OS_CMD_EXE 2
+} chaz_OS = { "", "", "", "", "", "", 0 };
void
chaz_OS_init(void) {
@@ -2591,16 +3127,43 @@ chaz_OS_init(void) {
/* Detect shell based on whether the bitbucket is "/dev/null" or "nul". */
if (chaz_Util_can_open_file("/dev/null")) {
+ char *uname;
+ size_t uname_len;
+ size_t i;
+
+ chaz_OS.shell_type = CHAZ_OS_POSIX;
+
+ /* Detect Unix name. */
+ uname = chaz_OS_run_and_capture("uname", &uname_len);
+ for (i = 0; i < CHAZ_OS_NAME_MAX && i < uname_len; i++) {
+ char c = uname[i];
+ if (!c || isspace(c)) { break; }
+ chaz_OS.name[i] = tolower(c);
+ }
+ if (i > 0) { chaz_OS.name[i] = '\0'; }
+ else { strcpy(chaz_OS.name, "unknown_unix"); }
+ free(uname);
+
strcpy(chaz_OS.dev_null, "/dev/null");
strcpy(chaz_OS.exe_ext, "");
strcpy(chaz_OS.obj_ext, ".o");
+ if (memcmp(chaz_OS.name, "darwin", 6) == 0) {
+ strcpy(chaz_OS.shared_obj_ext, ".dylib");
+ }
+ else if (memcmp(chaz_OS.name, "cygwin", 6) == 0) {
+ strcpy(chaz_OS.shared_obj_ext, ".dll");
+ }
+ else {
+ strcpy(chaz_OS.shared_obj_ext, ".so");
+ }
strcpy(chaz_OS.local_command_start, "./");
- chaz_OS.shell_type = CHAZ_OS_POSIX;
}
else if (chaz_Util_can_open_file("nul")) {
+ strcpy(chaz_OS.name, "windows");
strcpy(chaz_OS.dev_null, "nul");
strcpy(chaz_OS.exe_ext, ".exe");
strcpy(chaz_OS.obj_ext, ".obj");
+ strcpy(chaz_OS.shared_obj_ext, ".dll");
strcpy(chaz_OS.local_command_start, ".\\");
chaz_OS.shell_type = CHAZ_OS_CMD_EXE;
}
@@ -2611,6 +3174,21 @@ chaz_OS_init(void) {
}
const char*
+chaz_OS_name(void) {
+ return chaz_OS.name;
+}
+
+int
+chaz_OS_is_darwin(void) {
+ return memcmp(chaz_OS.name, "darwin", 6) == 0;
+}
+
+int
+chaz_OS_is_cygwin(void) {
+ return memcmp(chaz_OS.name, "cygwin", 6) == 0;
+}
+
+const char*
chaz_OS_exe_ext(void) {
return chaz_OS.exe_ext;
}
@@ -2621,11 +3199,21 @@ chaz_OS_obj_ext(void) {
}
const char*
+chaz_OS_shared_obj_ext(void) {
+ return chaz_OS.shared_obj_ext;
+}
+
+const char*
chaz_OS_dev_null(void) {
return chaz_OS.dev_null;
}
int
+chaz_OS_shell_type(void) {
+ return chaz_OS.shell_type;
+}
+
+int
chaz_OS_remove(const char *name) {
/*
* On Windows it can happen that another process, typically a
@@ -2699,6 +3287,15 @@ chaz_OS_run_redirected(const char *command, const char *path) {
return retval;
}
+char*
+chaz_OS_run_and_capture(const char *command, size_t *output_len) {
+ char *output;
+ chaz_OS_run_redirected(command, CHAZ_OS_TARGET_PATH);
+ output = chaz_Util_slurp_file(CHAZ_OS_TARGET_PATH, output_len);
+ chaz_Util_remove_and_verify(CHAZ_OS_TARGET_PATH);
+ return output;
+}
+
void
chaz_OS_mkdir(const char *filepath) {
char *command = NULL;