summaryrefslogtreecommitdiff
path: root/pjsip-apps/src/swig/python/cc_mingw.c
blob: adbcf8a4710874060e3919d8f2e7ff6642b95f51 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/* $Id$ */
/*
 * Copyright (C) 2014 Teluu Inc. (http://www.teluu.com)
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

/* A simple proxy application for gcc/g++ to remove '-mno-cygwin' option
 * when the gcc/g++ doesn't support it. Note that the option seems to be
 * deprecated and causing compile error since gcc 4.7, while the option is
 * auto-generated by Python distutils module (up to Python 2.7) in building
 * Python extension.
 */
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

/* Find gcc executable from env var PATH but omitting current dir */
const char* find_gcc(const char *gcc_exe)
{
    static char fname[256];
    char spath[1024 * 10];
    char *p;

    p = getenv("PATH");
    if (strlen(p) >= sizeof(spath)) {
	printf("Error: find_gcc() not enough buffer 1\n");
	return NULL;
    }

    strncpy(spath, p, sizeof(spath));
    p = strtok(spath, ";");
    while (p) {
	int len;

	/* Skip current dir */
	if (strcmp(p, ".") == 0) {
	    p = strtok(NULL, ";");
	    continue;
	}

	len = snprintf(fname, sizeof(fname), "%s\\%s", p, gcc_exe);
	if (len < 0 || len >= sizeof(fname)) {
	    printf("Error: find_gcc() not enough buffer 2\n");
	    return NULL;
	}

	if (access(fname, F_OK | X_OK) != -1) {
	    return fname;
	}
	
	p = strtok(NULL, ";");
    }

    return NULL;
}

int check_gcc_reject_mno_cygwin(const char *gcc_path)
{
    FILE *fp;
    char tmp[1024];
    const char *p;
    int ver;

    snprintf(tmp, sizeof(tmp), "%s -mno-cygwin 2>&1", gcc_path);
    fp = popen(tmp, "r");
    if (fp == NULL) {
	printf("Error: failed to run gcc\n" );
	return -1;
    }

    while (fgets(tmp, sizeof(tmp), fp) != NULL) {
	if (strstr(tmp, "unrecognized") && strstr(tmp, "-mno-cygwin"))
	    return 1;
    }

    pclose(fp);
    return 0;
}

int main(int argc, const char const **argv)
{
    const char *app = "python cc_mingw.py";
    char cmd[1024 * 8], *p;
    int i, len, sz;
    int ret = 0;
    const char *spath, *gcc_exe;
    int remove_mno_cygwin;

    if (strstr(argv[0], "gcc") || strstr(argv[0], "GCC")) {
	gcc_exe = "gcc.exe";
    } else if (strstr(argv[0], "g++") || strstr(argv[0], "G++")) {
	gcc_exe = "g++.exe";
    } else {
	printf("Error: app name not gcc/g++\n");
	return -10;
    }

    /* Resolve GCC path from PATH env var */
    gcc_exe = find_gcc(gcc_exe);
    if (!gcc_exe) {
	printf("Error: real gcc/g++ not found\n");
	return -20;
    }

    /* Check if GCC rejects '-mno-cygwin' option */
    remove_mno_cygwin = check_gcc_reject_mno_cygwin(gcc_exe);
    if (remove_mno_cygwin < 0)
	return -30;
    
    len = snprintf(cmd, sizeof(cmd), "%s", gcc_exe);
    p = cmd + len;
    sz = sizeof(cmd) - len;
    for (i = 1; i < argc && sz > 0; ++i) {

	if (remove_mno_cygwin && strcmp(argv[i], "-mno-cygwin") == 0) {
	    printf("Removed option '-mno-cygwin'.\n");
	    continue;
	}

	len = snprintf(p, sz, " %s", argv[i]);
	if (len < 0 || len >= sz) {
	    ret = E2BIG;
	    break;
	}
	p += len;
	sz -= len;
    }
    
    if (!ret) {
	//printf("cmd = %s\n", cmd);
	ret = system(cmd);
    }

    if (ret) {
        printf("Error: %s\n", strerror(ret));
    }
    
    return ret;
}