Move common code to a separate file

This commit is contained in:
Pierre Pronchery 2017-11-18 04:23:19 +01:00
parent f45fb01cb8
commit ce35c9c919
8 changed files with 176 additions and 101 deletions

100
src/common.c Normal file
View File

@ -0,0 +1,100 @@
/* $Id$ */
/* Copyright (c) 2017 Pierre Pronchery <khorben@defora.org> */
/* 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;
}

58
src/common.h Normal file
View File

@ -0,0 +1,58 @@
/* $Id$ */
/* Copyright (c) 2017 Pierre Pronchery <khorben@defora.org> */
/* 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 <System.h>
# 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 */

View File

@ -38,6 +38,7 @@
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#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;
}

View File

@ -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 */

View File

@ -37,6 +37,7 @@
#include <string.h>
#include <libgen.h>
#include <errno.h>
#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)

View File

@ -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

View File

@ -30,6 +30,7 @@
#define BASEDIR ".."
#include "../src/common.c"
#include "../src/configure.c"
#include "../src/makefile.c"
#include "../src/settings.c"

View File

@ -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