Refactor looking up programs

This paves the way for configuration files.
This commit is contained in:
Pierre Pronchery 2017-11-09 04:34:15 +01:00
parent 6855258c8c
commit 666bcc29c0
2 changed files with 67 additions and 79 deletions

View File

@ -60,21 +60,7 @@ struct _Configure {
char const * exeext;
char const * soext;
} extensions;
struct
{
char const * ar;
char const * as;
char const * cc;
char const * ccshared;
char const * cxx;
char const * install;
char const * libtool;
char const * ln;
char const * mkdir;
char const * ranlib;
char const * rm;
char const * tar;
} programs;
Config * programs;
};
@ -208,7 +194,7 @@ unsigned int enum_string_short(unsigned int last, const String * strings[],
static void _configure_detect(Configure * configure);
static HostKernel _detect_kernel(HostOS os, char const * release);
static void _configure_detect_extensions(Configure * configure);
static void _configure_detect_programs(Configure * configure);
static int _configure_detect_programs(Configure * configure);
static int _configure_load(ConfigurePrefs * prefs, char const * directory,
configArray * ca);
static int _load_subdirs(ConfigurePrefs * prefs, char const * directory,
@ -231,8 +217,8 @@ int configure(ConfigurePrefs * prefs, String const * directory)
cfgr.prefs = prefs;
_configure_detect(&cfgr);
_configure_detect_extensions(&cfgr);
_configure_detect_programs(&cfgr);
if((ret = _configure_load(prefs, directory, ca)) == 0)
if((ret = _configure_detect_programs(&cfgr)) != 0
|| (ret = _configure_load(prefs, directory, ca)) == 0)
{
if(prefs->flags & PREFS_n)
ret = _configure_do(&cfgr, ca);
@ -252,6 +238,8 @@ int configure(ConfigurePrefs * prefs, String const * directory)
config_delete(p);
}
array_delete(ca);
if(cfgr.programs != NULL)
config_delete(cfgr.programs);
return ret;
}
@ -331,34 +319,47 @@ static void _configure_detect_extensions(Configure * configure)
}
}
static void _configure_detect_programs(Configure * configure)
static int _configure_detect_programs(Configure * configure)
{
configure->programs.ar = "ar";
configure->programs.as = "as";
configure->programs.cc = "cc";
configure->programs.ccshared = "$(CC) -shared";
configure->programs.cxx = "c++";
configure->programs.install = "install";
configure->programs.libtool = "libtool";
configure->programs.ln = "ln -f";
configure->programs.mkdir = "mkdir -m 0755 -p";
configure->programs.ranlib = "ranlib";
configure->programs.rm = "rm -f";
configure->programs.tar = "tar";
int ret = 0;
String const section[] = "programs";
struct
{
String const * name;
String const * program;
} programs[] =
{
{ "ccshared", "$(CC) -shared" },
{ "cxx", "c++" },
{ "ln", "ln -f" },
{ "mkdir", "mkdir -m 0755 -p" },
{ "rm", "rm -f" },
};
size_t i;
if((configure->programs = config_new()) == NULL)
return -1;
for(i = 0; i < sizeof(programs) / sizeof(*programs); i++)
if(config_set(configure->programs, section, programs[i].name,
programs[i].program) != 0)
return -1;
/* platform-specific */
switch(configure->os)
{
case HO_MACOSX:
configure->programs.ccshared = "$(CC) -dynamiclib";
ret = config_set(configure->programs, section,
"ccshared", "$(CC) -dynamiclib");
break;
case HO_WIN32:
configure->programs.ccshared = "$(CC) -shared"
ret = config_set(configure->programs, section,
"ccshared", "$(CC) -shared"
" -Wl,-no-undefined"
" -Wl,--enable-runtime-pseudo-reloc";
" -Wl,--enable-runtime-pseudo-reloc");
break;
default:
break;
}
return ret;
}
static int _configure_load(ConfigurePrefs * prefs, String const * directory,
@ -513,32 +514,12 @@ ConfigurePrefs const * configure_get_prefs(Configure * configure)
/* configure_get_program */
String const * configure_get_program(Configure * configure, String const * name)
{
/* XXX use a configuration file instead */
if(string_compare(name, "AR") == 0)
return configure->programs.ar;
else if(string_compare(name, "AS") == 0)
return configure->programs.as;
else if(string_compare(name, "CC") == 0)
return configure->programs.cc;
else if(string_compare(name, "CCSHARED") == 0)
return configure->programs.ccshared;
else if(string_compare(name, "CXX") == 0)
return configure->programs.cxx;
else if(string_compare(name, "INSTALL") == 0)
return configure->programs.install;
else if(string_compare(name, "LIBTOOL") == 0)
return configure->programs.libtool;
else if(string_compare(name, "LN") == 0)
return configure->programs.ln;
else if(string_compare(name, "MKDIR") == 0)
return configure->programs.mkdir;
else if(string_compare(name, "RANLIB") == 0)
return configure->programs.ranlib;
else if(string_compare(name, "RM") == 0)
return configure->programs.rm;
else if(string_compare(name, "TAR") == 0)
return configure->programs.tar;
return "";
String const section[] = "programs";
String const * program;
if((program = config_get(configure->programs, section, name)) != NULL)
return program;
return name;
}

View File

@ -275,9 +275,9 @@ static int _variables_dist(Configure * configure, FILE * fp)
_makefile_output_variable(fp, "DESTDIR",
prefs->destdir);
}
_makefile_output_program(configure, fp, "MKDIR");
_makefile_output_program(configure, fp, "INSTALL");
_makefile_output_program(configure, fp, "RM");
_makefile_output_program(configure, fp, "mkdir");
_makefile_output_program(configure, fp, "install");
_makefile_output_program(configure, fp, "rm");
break;
}
if(c == '\0')
@ -440,19 +440,19 @@ static int _variables_executables(Configure * configure, FILE * fp)
}
if(targets != NULL || includes != NULL || package != NULL)
{
_makefile_output_program(configure, fp, "RM");
_makefile_output_program(configure, fp, "LN");
_makefile_output_program(configure, fp, "rm");
_makefile_output_program(configure, fp, "ln");
}
if(package != NULL)
{
_makefile_output_program(configure, fp, "TAR");
_makefile_output_program(configure, fp, "MKDIR");
_makefile_output_program(configure, fp, "tar");
_makefile_output_program(configure, fp, "mkdir");
}
if(targets != NULL || includes != NULL)
{
if(package == NULL)
_makefile_output_program(configure, fp, "MKDIR");
_makefile_output_program(configure, fp, "INSTALL");
_makefile_output_program(configure, fp, "mkdir");
_makefile_output_program(configure, fp, "install");
}
return 0;
}
@ -566,7 +566,7 @@ static void _targets_asflags(Configure * configure, FILE * fp)
if(as != NULL || asff != NULL || asf != NULL)
{
_makefile_output_variable(fp, "AS", (as != NULL) ? as
: configure_get_program(configure, "AS"));
: configure_get_program(configure, "as"));
_makefile_output_variable(fp, "ASFLAGSF", asff);
_makefile_output_variable(fp, "ASFLAGS", asf);
}
@ -590,7 +590,7 @@ static void _targets_cflags(Configure * configure, FILE * fp)
&& cc == NULL)
return;
if(cc == NULL)
_makefile_output_program(configure, fp, "CC");
_makefile_output_program(configure, fp, "cc");
else
_makefile_output_variable(fp, "CC", cc);
_makefile_output_variable(fp, "CPPFLAGSF", cppf);
@ -621,7 +621,7 @@ static void _targets_cxxflags(Configure * configure, FILE * fp)
if(cxx != NULL || cxxff != NULL || cxxf != NULL)
{
if(cxx == NULL)
cxx = configure_get_program(configure, "CXX");
cxx = configure_get_program(configure, "cxx");
_makefile_output_variable(fp, "CXX", cxx);
}
if(cxxff != NULL)
@ -785,7 +785,7 @@ static void _variables_library(Configure * configure, FILE * fp, char * done)
if(configure_can_library_static(configure))
_variables_library_static(configure, fp);
if((p = _makefile_get_config(configure, NULL, "ld")) == NULL)
_makefile_output_program(configure, fp, "CCSHARED");
_makefile_output_program(configure, fp, "ccshared");
else
_makefile_output_variable(fp, "CCSHARED", p);
_makefile_output_variable(fp, "SOEXT", configure_get_soext(configure));
@ -796,12 +796,12 @@ static void _variables_library_static(Configure * configure, FILE * fp)
String const * p;
if((p = _makefile_get_config(configure, NULL, "ar")) == NULL)
_makefile_output_program(configure, fp, "AR");
_makefile_output_program(configure, fp, "ar");
else
_makefile_output_variable(fp, "AR", p);
_makefile_output_variable(fp, "ARFLAGS", "-rc");
if((p = _makefile_get_config(configure, NULL, "ranlib")) == NULL)
_makefile_output_program(configure, fp, "RANLIB");
_makefile_output_program(configure, fp, "ranlib");
else
_makefile_output_variable(fp, "RANLIB", p);
}
@ -815,7 +815,7 @@ static void _variables_libtool(Configure * configure, FILE * fp, char * done)
{
if((p = _makefile_get_config(configure, NULL, "libtool"))
== NULL)
_makefile_output_program(configure, fp, "LIBTOOL");
_makefile_output_program(configure, fp, "libtool");
else
_makefile_output_variable(fp, "LIBTOOL", p);
}
@ -880,7 +880,7 @@ static int _variables_subdirs(Configure * configure, FILE * fp)
if(q != NULL)
return 0;
}
return _makefile_output_program(configure, fp, "MKDIR");
return _makefile_output_program(configure, fp, "mkdir");
}
static int _targets_all(Configure * configure, FILE * fp);
@ -2810,10 +2810,17 @@ static int _makefile_link(FILE * fp, int symlink, char const * link,
static int _makefile_output_program(Configure * configure, FILE * fp,
String const * name)
{
int ret;
String const * value;
String * upper;
if((upper = string_new(name)) == NULL)
return -1;
string_toupper(upper);
value = configure_get_program(configure, name);
return _makefile_output_variable(fp, name, value);
ret = _makefile_output_variable(fp, upper, value);
string_delete(upper);
return ret;
}