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

View File

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