📄 aplibtool.c
字号:
void post_parse_fixup(cmd_data_t *cmd_data)
{
int a;
char *arg;
char *ext;
if (cmd_data->output_type == otStaticLibrary && cmd_data->mode == mLink) {
/* We do a real hatchet job on the args when making a static library
* removing all compiler switches & any other cruft that ar won't like
* We also need to explode any libraries listed
*/
for (a=0; a < cmd_data->num_args; a++) {
arg = cmd_data->arglist[a];
if (arg) {
ext = strrchr(arg, '.');
if (ext) {
ext++;
}
if (arg[0] == '-') {
cmd_data->arglist[a] = NULL;
if (strcmp(arg, "-rpath") == 0 && a+1 < cmd_data->num_args) {
cmd_data->arglist[a+1] = NULL;
}
if (strcmp(arg, "-R") == 0 && a+1 < cmd_data->num_args) {
cmd_data->arglist[a+1] = NULL;
}
if (strcmp(arg, "-version-info") == 0 && a+1 < cmd_data->num_args) {
cmd_data->arglist[a+1] = NULL;
}
if (strcmp(arg, "-Zstack") == 0 && a+1 < cmd_data->num_args) {
cmd_data->arglist[a+1] = NULL;
}
if (strcmp(arg, "-o") == 0) {
a++;
}
}
if (strcmp(arg, CC) == 0 || strcmp(arg, CC EXE_EXT) == 0) {
cmd_data->arglist[a] = LIBRARIAN " cr";
}
if (ext) {
if (strcmp(ext, "h") == 0 || strcmp(ext, "c") == 0) {
/* ignore source files, they don't belong in a library */
cmd_data->arglist[a] = NULL;
}
if (strcmp(ext, STATIC_LIB_EXT) == 0) {
cmd_data->arglist[a] = NULL;
explode_static_lib(arg, cmd_data);
}
}
}
}
}
if (cmd_data->output_type == otDynamicLibrary) {
for (a=0; a < cmd_data->num_args; a++) {
arg = cmd_data->arglist[a];
if (arg) {
if (strcmp(arg, "-rpath") == 0 && a+1 < cmd_data->num_args) {
cmd_data->arglist[a] = NULL;
cmd_data->arglist[a+1] = NULL;
}
}
}
if (export_all) {
generate_def_file(cmd_data);
}
}
#if USE_OMF
if (cmd_data->output_type == otObject ||
cmd_data->output_type == otProgram ||
cmd_data->output_type == otDynamicLibrary) {
cmd_data->arglist[cmd_data->num_args++] = "-Zomf";
}
#endif
if (shared && (cmd_data->output_type == otObject || cmd_data->output_type == otDynamicLibrary)) {
cmd_data->arglist[cmd_data->num_args++] = SHARE_SW;
}
}
int execute_command(cmd_data_t *cmd_data)
{
int target = 0;
char *command;
int a, total_len = 0;
char *args[4];
for (a=0; a < cmd_data->num_args; a++) {
if (cmd_data->arglist[a]) {
total_len += strlen(cmd_data->arglist[a]) + 1;
}
}
command = (char *)malloc( total_len );
command[0] = 0;
for (a=0; a < cmd_data->num_args; a++) {
if (cmd_data->arglist[a]) {
strcat(command, cmd_data->arglist[a]);
strcat(command, " ");
}
}
command[strlen(command)-1] = 0;
if (!silent) {
puts(command);
}
cmd_data->num_args = target;
cmd_data->arglist[cmd_data->num_args] = NULL;
command = shell_esc(command);
args[0] = SHELL_CMD;
args[1] = "-c";
args[2] = command;
args[3] = NULL;
return spawnvp(P_WAIT, args[0], args);
}
char *shell_esc(const char *str)
{
char *cmd;
unsigned char *d;
const unsigned char *s;
cmd = (char *)malloc(2 * strlen(str) + 1);
d = (unsigned char *)cmd;
s = (const unsigned char *)str;
for (; *s; ++s) {
if (*s == '"' || *s == '\\') {
*d++ = '\\';
}
*d++ = *s;
}
*d = '\0';
return cmd;
}
bool explode_static_lib(char *lib, cmd_data_t *cmd_data)
{
char tmpdir[1024];
char savewd[1024];
char cmd[1024];
char *name;
DIR *dir;
struct dirent *entry;
strcpy(tmpdir, lib);
strcat(tmpdir, ".exploded");
mkdir(tmpdir, 0);
cmd_data->tmp_dirs[cmd_data->num_tmp_dirs++] = strdup(tmpdir);
getcwd(savewd, sizeof(savewd));
if (chdir(tmpdir) != 0)
return false;
strcpy(cmd, LIBRARIAN " x ");
name = strrchr(lib, '/');
if (name) {
name++;
} else {
name = lib;
}
strcat(cmd, "../");
strcat(cmd, name);
system(cmd);
chdir(savewd);
dir = opendir(tmpdir);
while ((entry = readdir(dir)) != NULL) {
if (entry->d_name[0] != '.') {
strcpy(cmd, tmpdir);
strcat(cmd, "/");
strcat(cmd, entry->d_name);
cmd_data->arglist[cmd_data->num_args++] = strdup(cmd);
}
}
closedir(dir);
return true;
}
void cleanup_tmp_dir(char *dirname)
{
DIR *dir;
struct dirent *entry;
char fullname[1024];
dir = opendir(dirname);
if (dir == NULL)
return;
while ((entry = readdir(dir)) != NULL) {
if (entry->d_name[0] != '.') {
strcpy(fullname, dirname);
strcat(fullname, "/");
strcat(fullname, entry->d_name);
remove(fullname);
}
}
rmdir(dirname);
}
void cleanup_tmp_dirs(cmd_data_t *cmd_data)
{
int d;
for (d=0; d < cmd_data->num_tmp_dirs; d++) {
cleanup_tmp_dir(cmd_data->tmp_dirs[d]);
}
}
void generate_def_file(cmd_data_t *cmd_data)
{
char def_file[1024];
char implib_file[1024];
char *ext;
FILE *hDef;
char *export_args[1024];
int num_export_args = 0;
char *cmd;
int cmd_size = 0;
int a;
if (cmd_data->output_name) {
strcpy(def_file, cmd_data->output_name);
strcat(def_file, ".def");
hDef = fopen(def_file, "w");
if (hDef != NULL) {
fprintf(hDef, "LIBRARY '%s' INITINSTANCE\n", nameof(cmd_data->output_name));
fprintf(hDef, "DATA NONSHARED\n");
fprintf(hDef, "EXPORTS\n");
fclose(hDef);
for (a=0; a < cmd_data->num_obj_files; a++) {
cmd_size += strlen(cmd_data->obj_files[a]) + 1;
}
cmd_size += strlen(GEN_EXPORTS) + strlen(def_file) + 3;
cmd = (char *)malloc(cmd_size);
strcpy(cmd, GEN_EXPORTS);
for (a=0; a < cmd_data->num_obj_files; a++) {
strcat(cmd, " ");
strcat(cmd, cmd_data->obj_files[a] );
}
strcat(cmd, ">>");
strcat(cmd, def_file);
puts(cmd);
export_args[num_export_args++] = SHELL_CMD;
export_args[num_export_args++] = "-c";
export_args[num_export_args++] = cmd;
export_args[num_export_args++] = NULL;
spawnvp(P_WAIT, export_args[0], export_args);
cmd_data->arglist[cmd_data->num_args++] = strdup(def_file);
/* Now make an import library for the dll */
num_export_args = 0;
export_args[num_export_args++] = DEF2IMPLIB_CMD;
export_args[num_export_args++] = "-o";
strcpy(implib_file, ".libs/");
strcat(implib_file, cmd_data->stub_name);
ext = strrchr(implib_file, '.');
if (ext)
*ext = 0;
strcat(implib_file, ".");
strcat(implib_file, STATIC_LIB_EXT);
export_args[num_export_args++] = implib_file;
export_args[num_export_args++] = def_file;
export_args[num_export_args++] = NULL;
spawnvp(P_WAIT, export_args[0], export_args);
}
}
}
/* returns just a file's name without path or extension */
char *nameof(char *fullpath)
{
char buffer[1024];
char *ext;
char *name = strrchr(fullpath, '/');
if (name == NULL) {
name = strrchr(fullpath, '\\');
}
if (name == NULL) {
name = fullpath;
} else {
name++;
}
strcpy(buffer, name);
ext = strrchr(buffer, '.');
if (ext) {
*ext = 0;
return strdup(buffer);
}
return name;
}
char *truncate_dll_name(char *path)
{
/* Cut DLL name down to 8 characters after removing any mod_ prefix */
char *tmppath = strdup(path);
char *newname = strrchr(tmppath, '/') + 1;
char *ext = strrchr(tmppath, '.');
int len;
if (ext == NULL)
return tmppath;
len = ext - newname;
if (strncmp(newname, "mod_", 4) == 0) {
strcpy(newname, newname + 4);
len -= 4;
}
if (len > 8) {
strcpy(newname + 8, strchr(newname, '.'));
}
return tmppath;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -