Generate Makefile for C bindings
Project: http://git-wip-us.apache.org/repos/asf/lucy/repo Commit: http://git-wip-us.apache.org/repos/asf/lucy/commit/aaef9f1f Tree: http://git-wip-us.apache.org/repos/asf/lucy/tree/aaef9f1f Diff: http://git-wip-us.apache.org/repos/asf/lucy/diff/aaef9f1f Branch: refs/heads/master Commit: aaef9f1fe866fce32d74f6e3ddcb8bbd43680c73 Parents: c822e10 Author: Nick Wellnhofer <[email protected]> Authored: Sat Jan 5 23:14:51 2013 +0100 Committer: Nick Wellnhofer <[email protected]> Committed: Sat Mar 9 17:51:54 2013 +0100 ---------------------------------------------------------------------- c/.gitignore | 5 + c/cfc_header | 23 +++++ c/configure | 51 ++++++++++ common/charmonizer.main | 210 ++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 289 insertions(+), 0 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/lucy/blob/aaef9f1f/c/.gitignore ---------------------------------------------------------------------- diff --git a/c/.gitignore b/c/.gitignore new file mode 100644 index 0000000..1076813 --- /dev/null +++ b/c/.gitignore @@ -0,0 +1,5 @@ +/Makefile +/autogen/ +/charmonizer +/charmony.h +/liblucy.dylib http://git-wip-us.apache.org/repos/asf/lucy/blob/aaef9f1f/c/cfc_header ---------------------------------------------------------------------- diff --git a/c/cfc_header b/c/cfc_header new file mode 100644 index 0000000..d982cf9 --- /dev/null +++ b/c/cfc_header @@ -0,0 +1,23 @@ +/*********************************************** + + !!!! DO NOT EDIT !!!! + + This file was auto-generated by cfc. + + ***********************************************/ + +/* Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ http://git-wip-us.apache.org/repos/asf/lucy/blob/aaef9f1f/c/configure ---------------------------------------------------------------------- diff --git a/c/configure b/c/configure new file mode 100755 index 0000000..9206eb4 --- /dev/null +++ b/c/configure @@ -0,0 +1,51 @@ +#!/bin/sh + +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. +# The ASF licenses this file to You under the Apache License, Version 2.0 +# (the "License"); you may not use this file except in compliance with +# the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +echo Configuring cfc +(cd ../clownfish/compiler/c && ./configure) +echo + +probe_clang() { clang -v; } +probe_gcc() { gcc -v; } + +if [ -z "$CC" ]; then + case $(uname) in + Darwin*) compilers="clang gcc";; + *) compilers="gcc clang";; + esac + + for compiler in $compilers; do + if probe_$compiler >/dev/null 2>&1; then + CC=$compiler + break + fi + done + + if [ -z "$CC" ]; then + CC=cc + fi +fi + +echo "Using C compiler '$CC'" + +command="$CC ../common/charmonizer.c -o charmonizer" +echo $command +$command || exit + +echo Running charmonizer +./charmonizer --cc=$CC --enable-c --enable-makefile $@ + http://git-wip-us.apache.org/repos/asf/lucy/blob/aaef9f1f/common/charmonizer.main ---------------------------------------------------------------------- diff --git a/common/charmonizer.main b/common/charmonizer.main index ba36d25..8701d99 100644 --- a/common/charmonizer.main +++ b/common/charmonizer.main @@ -38,6 +38,13 @@ #include "Charmonizer/Core/ConfWriterPerl.h" #include "Charmonizer/Core/ConfWriterRuby.h" +#define DIR_SEP "/" + +typedef struct SourceFileContext { + chaz_MakeVar *var; + const char *dir; +} SourceFileContext; + static void S_add_compiler_flags(struct chaz_CLIArgs *args) { if (chaz_Probe_gcc_version_num()) { @@ -84,6 +91,199 @@ S_add_compiler_flags(struct chaz_CLIArgs *args) { } } +static void +S_source_file_callback(char *file, void *context) { + SourceFileContext *sfc = (SourceFileContext*)context; + const char *json_parser_c = "Lucy" DIR_SEP "Util" DIR_SEP "Json" DIR_SEP + "JsonParser.c"; + size_t file_len = strlen(file); + size_t obj_file_size; + const char *pattern; + char *obj_file; + + if (strcmp(file, json_parser_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 = "%s" DIR_SEP "%s$(OBJ_EXT)"; + obj_file_size = strlen(pattern) + file_len + 10; + obj_file = (char*)malloc(obj_file_size); + sprintf(obj_file, pattern, sfc->dir, file); + chaz_MakeVar_append(sfc->var, obj_file); + free(obj_file); +} + +static void +S_write_makefile() { + SourceFileContext sfc; + + const char *base_dir = ".."; + const char *exe_ext = chaz_OS_exe_ext(); + const char *obj_ext = chaz_OS_obj_ext(); + const char *shobj_ext = chaz_OS_shared_obj_ext(); + + const char *json_parser_y = "$(CORE_DIR)" DIR_SEP "Lucy" DIR_SEP "Util" + DIR_SEP "Json" DIR_SEP "JsonParser.y"; + const char *json_parser_h = "$(CORE_DIR)" DIR_SEP "Lucy" DIR_SEP "Util" + DIR_SEP "Json" DIR_SEP "JsonParser.h"; + const char *json_parser_c = "$(CORE_DIR)" DIR_SEP "Lucy" DIR_SEP "Util" + DIR_SEP "Json" DIR_SEP "JsonParser.c"; + + char *scratch; + + chaz_MakeFile *makefile; + chaz_MakeVar *var; + chaz_MakeRule *rule; + + printf("Creating Makefile...\n"); + + makefile = chaz_MakeFile_new(); + + /* Directories */ + + chaz_MakeFile_add_var(makefile, "SRC_DIR", "src"); + chaz_MakeFile_add_var(makefile, "AUTOGEN_DIR", "autogen"); + chaz_MakeFile_add_var(makefile, "BASE_DIR", base_dir); + chaz_MakeFile_add_var(makefile, "CORE_DIR", + "$(BASE_DIR)" DIR_SEP "core"); + chaz_MakeFile_add_var(makefile, "MODULES_DIR", + "$(BASE_DIR)" DIR_SEP "modules"); + chaz_MakeFile_add_var(makefile, "LEMON_DIR", + "$(BASE_DIR)" DIR_SEP "lemon"); + chaz_MakeFile_add_var(makefile, "CFC_DIR", + "$(BASE_DIR)" DIR_SEP "clownfish" DIR_SEP "compiler" + DIR_SEP "c"); + chaz_MakeFile_add_var(makefile, "SNOWSTEM_DIR", + "$(MODULES_DIR)" DIR_SEP "analysis" DIR_SEP + "snowstem" DIR_SEP "source"); + chaz_MakeFile_add_var(makefile, "SNOWSTOP_DIR", + "$(MODULES_DIR)" DIR_SEP "analysis" DIR_SEP + "snowstop" DIR_SEP "source"); + chaz_MakeFile_add_var(makefile, "UTF8PROC_DIR", + "$(MODULES_DIR)" DIR_SEP "unicode" DIR_SEP + "utf8proc"); + + /* File extensions */ + + chaz_MakeFile_add_var(makefile, "EXE_EXT", exe_ext); + chaz_MakeFile_add_var(makefile, "OBJ_EXT", obj_ext); + chaz_MakeFile_add_var(makefile, "SHOBJ_EXT", shobj_ext); + + /* C compiler */ + + chaz_MakeFile_add_var(makefile, "CC", chaz_CC_get_cc()); + + if (chaz_CC_msvc_version_num()) { + chaz_CC_add_extra_cflags("/nologo"); + } + chaz_CC_set_optimization_level("2"); + chaz_CC_add_include_dir("."); + chaz_CC_add_include_dir("$(SRC_DIR)"); + chaz_CC_add_include_dir("$(CORE_DIR)"); + chaz_CC_add_include_dir("$(AUTOGEN_DIR)" DIR_SEP "include"); + chaz_CC_add_include_dir("$(SNOWSTEM_DIR)" DIR_SEP "include"); + chaz_CC_add_include_dir("$(MODULES_DIR)" DIR_SEP "unicode" DIR_SEP "ucd"); + chaz_CC_add_include_dir("$(UTF8PROC_DIR)"); + + 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()); + + /* Object files */ + + chaz_MakeFile_add_var(makefile, "LEMON_OBJS", + "$(LEMON_DIR)" DIR_SEP "lemon$(OBJ_EXT)"); + + var = chaz_MakeFile_add_var(makefile, "LUCY_OBJS", NULL); + sfc.var = var; + + sfc.dir = "$(SRC_DIR)"; + chaz_Make_list_files("src", "c", S_source_file_callback, &sfc); + + scratch = (char*)malloc(strlen(base_dir) + 20); + sprintf(scratch, "%s" DIR_SEP "core", base_dir); + sfc.dir = "$(CORE_DIR)"; + chaz_Make_list_files(scratch, "c", S_source_file_callback, &sfc); + free(scratch); + chaz_MakeVar_append(var, "$(CORE_DIR)" DIR_SEP "Lucy" DIR_SEP "Util" + DIR_SEP "Json" DIR_SEP "JsonParser$(OBJ_EXT)"); + + scratch = (char*)malloc(strlen(base_dir) + 80); + sprintf(scratch, "%s" DIR_SEP "modules" DIR_SEP "analysis" DIR_SEP + "snowstem" DIR_SEP "source", base_dir); + sfc.dir = "$(SNOWSTEM_DIR)"; + chaz_Make_list_files(scratch, "c", S_source_file_callback, &sfc); + free(scratch); + + scratch = (char*)malloc(strlen(base_dir) + 80); + sprintf(scratch, "%s" DIR_SEP "modules" DIR_SEP "analysis" DIR_SEP + "snowstop" DIR_SEP "source", base_dir); + sfc.dir = "$(SNOWSTOP_DIR)"; + chaz_Make_list_files(scratch, "c", S_source_file_callback, &sfc); + free(scratch); + + scratch = (char*)malloc(strlen(base_dir) + 80); + sprintf(scratch, "%s" DIR_SEP "modules" DIR_SEP "unicode" DIR_SEP + "utf8proc", base_dir); + sfc.dir = "$(UTF8PROC_DIR)"; + chaz_Make_list_files(scratch, "c", S_source_file_callback, &sfc); + free(scratch); + + chaz_MakeVar_append(var, "$(AUTOGEN_DIR)" DIR_SEP "source" DIR_SEP + "parcel$(OBJ_EXT)"); + + /* Executables */ + + chaz_MakeFile_add_var(makefile, "LEMON_EXE", + "$(LEMON_DIR)" DIR_SEP "lemon$(EXE_EXT)"); + chaz_MakeFile_add_var(makefile, "CFC_EXE", + "$(CFC_DIR)" DIR_SEP "cfc$(EXE_EXT)"); + + /* Shared library */ + + chaz_MakeFile_add_var(makefile, "LUCY_SHOBJ", "liblucy$(SHOBJ_EXT)"); + + /* Rules */ + + chaz_MakeFile_add_rule(makefile, "all", "$(LUCY_SHOBJ)"); + + chaz_MakeFile_add_exe(makefile, "$(LEMON_EXE)", "$(LEMON_OBJS)", ""); + + rule = chaz_MakeFile_add_rule(makefile, "$(CFC_EXE)", NULL); + chaz_MakeRule_add_command_make(rule, "$(CFC_DIR)", NULL); + + rule = chaz_MakeFile_add_rule(makefile, "$(AUTOGEN_DIR)", "$(CFC_EXE)"); + chaz_MakeRule_add_command(rule, "$(CFC_EXE) --source=$(CORE_DIR) " + "--dest=$(AUTOGEN_DIR) --header=cfc_header"); + + rule = chaz_MakeFile_add_rule(makefile, json_parser_c, NULL); + chaz_MakeRule_add_prereq(rule, "$(LEMON_EXE)"); + chaz_MakeRule_add_prereq(rule, json_parser_y); + scratch = (char*)malloc(strlen(json_parser_y) + 20); + sprintf(scratch, "$(LEMON_EXE) -q %s", json_parser_y); + chaz_MakeRule_add_command(rule, scratch); + free(scratch); + + rule = chaz_MakeFile_add_rule(makefile, "$(LUCY_OBJS)", NULL); + chaz_MakeRule_add_prereq(rule, json_parser_c); + chaz_MakeRule_add_prereq(rule, "$(AUTOGEN_DIR)"); + + chaz_MakeFile_add_shared_obj(makefile, "$(LUCY_SHOBJ)", "$(LUCY_OBJS)", + ""); + + chaz_MakeFile_add_to_cleanup(makefile, "$(LUCY_OBJS)"); + chaz_MakeFile_add_to_cleanup(makefile, json_parser_h); + chaz_MakeFile_add_to_cleanup(makefile, json_parser_c); + chaz_MakeFile_add_dir_to_cleanup(makefile, "$(AUTOGEN_DIR)"); + + chaz_MakeFile_write(makefile); +} + int main(int argc, const char **argv) { /* Initialize. */ { @@ -152,6 +352,16 @@ int main(int argc, const char **argv) { "#endif\n\n" ); + { + 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();
