struct / union in initializer, RFE #901.
[sdcc.git] / sdcc / sdas / linksrc / sdld.c
blob123bd9a8aaa6a4df602c6223fa0acc513fabf279
1 /* sdld.c
3 Copyright (C) 2009-2010 Borut Razem
5 This program is free software; you can redistribute it and/or modify it
6 under the terms of the GNU General Public License as published by the
7 Free Software Foundation; either version 3, or (at your option) any
8 later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>. */
18 #ifndef _WIN32
19 #include <libgen.h>
20 #endif
21 #include <stdlib.h>
22 #include <stdio.h>
23 #include <string.h>
24 #include <ctype.h>
26 #include "sdld.h"
28 #define NELEM(x) (sizeof (x) / sizeof (x)[0])
31 static int sdld = -1;
32 static enum sdld_target_e target = TARGET_ID_UNKNOWN;
35 static char
36 *program_name (char *path)
38 #ifdef _WIN32
39 static char fname[_MAX_FNAME];
40 char *p;
42 _splitpath (path, NULL, NULL, fname, NULL);
43 /* convert it to lower case:
44 on DOS and Windows 9x the file name in argv[0] is uppercase */
45 for (p = fname; '\0' != *p; ++p)
46 *p = tolower (*p);
47 return fname;
48 #else
49 return basename (path);
50 #endif
54 static void
55 check_init(void)
57 if (sdld == -1)
59 fprintf(stderr, "sdld_init not called!\n");
60 exit (1);
65 void
66 sdld_init (char *path)
68 struct tgt_s {
69 char *str;
70 enum sdld_target_e target;
71 } tgt[] = {
72 { "gb", TARGET_ID_GB, },
73 { "z80", TARGET_ID_Z80, },
74 { "z180", TARGET_ID_Z180, },
75 { "8051", TARGET_ID_8051, },
76 { "6808", TARGET_ID_6808, },
77 { "stm8", TARGET_ID_STM8, },
78 { "pdk", TARGET_ID_PDK, },
79 { "f8", TARGET_ID_F8, },
81 int i = NELEM (tgt);
83 char *progname = program_name (path);
84 if ((sdld = (strncmp(progname, "sdld", 4) == 0)) != 0)
86 /* exception: sdld is 8051 linker */
87 if (progname[4] == '\0')
88 target = TARGET_ID_8051;
89 else
91 for (i = 0; i < NELEM (tgt); ++i)
93 if (strstr(progname, tgt[i].str))
95 target = tgt[i].target;
96 break;
101 /* diagnostic message */
102 if (getenv ("SDLD_DIAG"))
104 printf ("sdld path: %s\n", path);
105 printf ("is sdld: %d\n", sdld);
106 if (sdld)
107 printf ("sdld target: %s\n", (i >= NELEM (tgt)) ? "8051" : tgt[i].str);
113 is_sdld(void)
115 check_init();
116 return sdld;
120 void
121 set_sdld_target(enum sdld_target_e trgt)
123 target = trgt;
127 enum sdld_target_e
128 get_sdld_target(void)
130 check_init();
131 return target;
136 is_sdld_target_z80_like(void)
138 check_init();
139 return target == TARGET_ID_Z80 || target == TARGET_ID_Z180 || target == TARGET_ID_GB;
144 is_sdld_target_8051_like(void)
146 check_init();
147 return target == TARGET_ID_8051;