diff --git a/src/common.c b/src/common.c new file mode 100644 index 0000000..378420c --- /dev/null +++ b/src/common.c @@ -0,0 +1,100 @@ +/* $Id$ */ +/* Copyright (c) 2017 Pierre Pronchery */ +/* This file is part of DeforaOS Devel configure */ +/* All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A + * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED + * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ + + + +#include "common.h" + + +/* public */ +/* functions */ +/* enum_map_find */ +unsigned int enum_map_find(unsigned int last, EnumMap const * map, + String const * str) +{ + unsigned int i; + + for(i = 0; map[i].value != last; i++) + if(string_compare(map[i].string, str) == 0) + return map[i].value; + return last; +} + + +/* enum_string */ +unsigned int enum_string(unsigned int last, const String * strings[], + String const * str) +{ + unsigned int i; + + for(i = 0; i < last; i++) + if(string_compare(strings[i], str) == 0) + return i; + return last; +} + + +/* enum_string_short */ +unsigned int enum_string_short(unsigned int last, const String * strings[], + String const * str) +{ + unsigned int i; + + for(i = 0; i < last; i++) + if(string_compare_length(strings[i], str, + string_length(strings[i])) == 0) + return i; + return last; +} + + +/* source_extension */ +String const * source_extension(String const * source) +{ + size_t len; + + for(len = string_length(source); len > 0; len--) + if(source[len - 1] == '.') + return &source[len]; + return NULL; +} + + +/* source_type */ +ObjectType source_type(String const * source) +{ + String const * extension; + size_t i; + + if((extension = source_extension(source)) == NULL) + extension = source; + for(i = 0; sExtensionType[i].extension != NULL; i++) + if(string_compare(sExtensionType[i].extension, extension) == 0) + return sExtensionType[i].type; + return OT_UNKNOWN; +} diff --git a/src/common.h b/src/common.h new file mode 100644 index 0000000..d4b6156 --- /dev/null +++ b/src/common.h @@ -0,0 +1,58 @@ +/* $Id$ */ +/* Copyright (c) 2017 Pierre Pronchery */ +/* This file is part of DeforaOS Devel configure */ +/* All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A + * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED + * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ + + + +#ifndef CONFIGURE_COMMON_H +# define CONFIGURE_COMMON_H + +# include +# include "configure.h" + + +/* public */ +/* types */ +typedef struct _EnumMap +{ + unsigned int value; + String const * string; +} EnumMap; + + +/* functions */ +unsigned int enum_map_find(unsigned int last, EnumMap const * map, + String const * str); +unsigned int enum_string(unsigned int last, const String * strings[], + String const * str); +unsigned int enum_string_short(unsigned int last, const String * strings[], + String const * str); + +String const * source_extension(String const * source); +ObjectType source_type(String const * source); + +#endif /* !CONFIGURE_COMMON_H */ diff --git a/src/configure.c b/src/configure.c index e312c95..cef1ace 100644 --- a/src/configure.c +++ b/src/configure.c @@ -38,6 +38,7 @@ #include #include #include +#include "common.h" #include "makefile.h" #include "configure.h" #include "../config.h" @@ -138,52 +139,7 @@ const struct ExtensionType _sExtensionType[] = const struct ExtensionType * sExtensionType = _sExtensionType; -/* prototypes */ -String const * _source_extension(String const * source); -ObjectType _source_type(String const * source); - - /* functions */ -/* enum_map_find */ -unsigned int enum_map_find(unsigned int last, EnumMap const * map, - String const * str) -{ - unsigned int i; - - for(i = 0; map[i].value != last; i++) - if(string_compare(map[i].string, str) == 0) - return map[i].value; - return last; -} - - -/* enum_string */ -unsigned int enum_string(unsigned int last, const String * strings[], - String const * str) -{ - unsigned int i; - - for(i = 0; i < last; i++) - if(string_compare(strings[i], str) == 0) - return i; - return last; -} - - -/* enum_string_short */ -unsigned int enum_string_short(unsigned int last, const String * strings[], - String const * str) -{ - unsigned int i; - - for(i = 0; i < last; i++) - if(string_compare_length(strings[i], str, - string_length(strings[i])) == 0) - return i; - return last; -} - - /* configure_new */ static void _new_detect(Configure * configure); static HostKernel _new_detect_kernel(HostOS os, char const * release); @@ -568,30 +524,3 @@ int configure_warning(int ret, char const * format, ...) va_end(ap); return ret; } - - -/* source_extension */ -String const * _source_extension(String const * source) -{ - size_t len; - - for(len = string_length(source); len > 0; len--) - if(source[len - 1] == '.') - return &source[len]; - return NULL; -} - - -/* source_type */ -ObjectType _source_type(String const * source) -{ - String const * extension; - size_t i; - - if((extension = _source_extension(source)) == NULL) - extension = source; - for(i = 0; sExtensionType[i].extension != NULL; i++) - if(string_compare(sExtensionType[i].extension, extension) == 0) - return sExtensionType[i].type; - return OT_UNKNOWN; -} diff --git a/src/configure.h b/src/configure.h index 7c17aa6..32820e7 100644 --- a/src/configure.h +++ b/src/configure.h @@ -37,12 +37,6 @@ /* types */ ARRAY(Config *, config) -typedef struct _EnumMap -{ - unsigned int value; - String const * string; -} EnumMap; - typedef enum _HostArch { HA_AMD64 = 0, @@ -174,15 +168,4 @@ int configure_warning(int ret, char const * format, ...); int configure_project(Configure * configure, String const * directory); -/* generic */ -unsigned int enum_map_find(unsigned int last, EnumMap const * map, - String const * str); -unsigned int enum_string(unsigned int last, const String * strings[], - String const * str); -unsigned int enum_string_short(unsigned int last, const String * strings[], - String const * str); - -String const * _source_extension(String const * source); -ObjectType _source_type(String const * source); - #endif /* !CONFIGURE_CONFIGURE_H */ diff --git a/src/makefile.c b/src/makefile.c index 5c71b57..9373005 100644 --- a/src/makefile.c +++ b/src/makefile.c @@ -37,6 +37,7 @@ #include #include #include +#include "common.h" #include "settings.h" #include "configure.h" #include "../config.h" @@ -998,7 +999,7 @@ static int _objs_source(Makefile * makefile, String * source, TargetType tt) String const * extension; size_t len; - if((extension = _source_extension(source)) == NULL) + if((extension = source_extension(source)) == NULL) { fprintf(stderr, "%s%s%s", PROGNAME ": ", source, ": no extension for source\n"); @@ -1006,7 +1007,7 @@ static int _objs_source(Makefile * makefile, String * source, TargetType tt) } len = string_length(source) - string_length(extension) - 1; source[len] = '\0'; - switch(_source_type(extension)) + switch(source_type(extension)) { case OT_ASM_SOURCE: case OT_C_SOURCE: @@ -1088,13 +1089,13 @@ static int _target_flags(Makefile * makefile, String const * target) continue; c = sources[i]; sources[i] = '\0'; - extension = _source_extension(sources); + extension = source_extension(sources); if(extension == NULL) { sources[i] = c; continue; } - type = _source_type(extension); + type = source_type(extension); if(!done[type]) switch(type) { @@ -1354,9 +1355,9 @@ static int _target_object(Makefile * makefile, ": An object can have only one source file\n"); return 1; } - if((extension = _source_extension(p)) == NULL) + if((extension = source_extension(p)) == NULL) return 1; - switch(_source_type(extension)) + switch(source_type(extension)) { case OT_ASM_SOURCE: _makefile_print(makefile, "\n%s%s%s%s\n%s%s", @@ -1636,11 +1637,11 @@ static int _target_source(Makefile * makefile, if((p = _makefile_get_config(makefile, target, "type")) != NULL) tt = enum_string(TT_LAST, sTargetType, p); - if((extension = _source_extension(source)) == NULL) + if((extension = source_extension(source)) == NULL) return 1; len = string_length(source) - string_length(extension) - 1; source[len] = '\0'; - switch((ot = _source_type(extension))) + switch((ot = source_type(extension))) { case OT_ASM_SOURCE: if(tt == TT_OBJECT) diff --git a/src/project.conf b/src/project.conf index 9ac6b7c..62189cc 100644 --- a/src/project.conf +++ b/src/project.conf @@ -5,18 +5,21 @@ cflags_force=`pkg-config --cflags libSystem` cflags=-W -Wall -g -O2 -fPIE -D_FORTIFY_SOURCE=2 -fstack-protector ldflags_force=`pkg-config --libs libSystem` ldflags=-pie -Wl,-z,relro -Wl,-z,now -dist=Makefile,configure.h,makefile.h,settings.h +dist=Makefile,common.h,configure.h,makefile.h,settings.h [configure] type=binary depends=$(OBJDIR)makefile.o -sources=configure.c,settings.c,main.c +sources=common.c,configure.c,settings.c,main.c ldflags=$(OBJDIR)makefile.o install=$(BINDIR) +[common.c] +depends=common.h + [configure.c] cppflags=-D PREFIX=\"$(PREFIX)\" -depends=configure.h,makefile.h,../config.h +depends=common.h,configure.h,makefile.h,../config.h [main.c] depends=configure.h diff --git a/tools/configure.c b/tools/configure.c index 2164b5f..ced22fd 100644 --- a/tools/configure.c +++ b/tools/configure.c @@ -30,6 +30,7 @@ #define BASEDIR ".." +#include "../src/common.c" #include "../src/configure.c" #include "../src/makefile.c" #include "../src/settings.c" diff --git a/tools/project.conf b/tools/project.conf index f783dae..8a52b8a 100644 --- a/tools/project.conf +++ b/tools/project.conf @@ -11,7 +11,7 @@ ldflags=`pkg-config --libs libSystem` sources=configure.c [configure.c] -depends=../src/configure.c,../src/configure.h,../src/main.c,../src/makefile.c,../src/makefile.h,../src/settings.c,../src/settings.h +depends=../src/common.c,../src/common.h,../src/configure.c,../src/configure.h,../src/main.c,../src/makefile.c,../src/makefile.h,../src/settings.c,../src/settings.h [pkg-config] type=binary