Imported Upstream version 1.7.2
[platform/upstream/edje.git] / src / bin / edje_cc.c
1 #ifdef HAVE_CONFIG_H
2 # include "config.h"
3 #endif
4
5 #include <string.h>
6 #include <locale.h>
7 #include <limits.h>
8 #include <sys/stat.h>
9
10 #include "edje_cc.h"
11 int _edje_cc_log_dom = -1;
12 static void main_help(void);
13
14 Eina_Prefix  *pfx = NULL;
15 Eina_List *snd_dirs = NULL;
16 Eina_List *img_dirs = NULL;
17 Eina_List *fnt_dirs = NULL;
18 Eina_List *defines = NULL;
19 char      *file_in = NULL;
20 char      *tmp_dir = NULL;
21 char      *file_out = NULL;
22 char      *watchfile = NULL;
23
24 static const char *progname = NULL;
25
26 int        no_lossy = 0;
27 int        no_comp = 0;
28 int        no_raw = 0;
29 int        no_save = 0;
30 int        min_quality = 0;
31 int        max_quality = 100;
32 int        compress_mode = EET_COMPRESSION_HI;
33 int        threads = 0;
34
35 static void
36 _edje_cc_log_cb(const Eina_Log_Domain *d,
37                 Eina_Log_Level level,
38                 const char *file,
39                 const char *fnc,
40                 int line,
41                 const char *fmt,
42                 __UNUSED__ void *data,
43                 va_list args)
44 {
45    if ((d->name) && (d->namelen == sizeof("edje_cc") - 1) &&
46        (memcmp(d->name, "edje_cc", sizeof("edje_cc") - 1) == 0))
47      {
48         const char *prefix;
49
50         eina_log_console_color_set(stderr, eina_log_level_color_get(level));
51         switch (level)
52           {
53            case EINA_LOG_LEVEL_CRITICAL:
54               prefix = "Critical. ";
55               break;
56            case EINA_LOG_LEVEL_ERR:
57               prefix = "Error. ";
58               break;
59            case EINA_LOG_LEVEL_WARN:
60               prefix = "Warning. ";
61               break;
62            default:
63               prefix = "";
64           }
65         fprintf(stderr, "%s: %s", progname, prefix);
66         eina_log_console_color_set(stderr, EINA_COLOR_RESET);
67
68         vfprintf(stderr, fmt, args);
69         putc('\n', stderr);
70      }
71    else
72      eina_log_print_cb_stderr(d, level, file, fnc, line, fmt, NULL, args);
73 }
74
75 static void
76 main_help(void)
77 {
78    printf
79      ("Usage:\n"
80       "\t%s [OPTIONS] input_file.edc [output_file.edj]\n"
81       "\n"
82       "Where OPTIONS is one or more of:\n"
83       "\n"
84       "-w files.txt             Dump all sources files path into files.txt\n"
85       "-id image/directory      Add a directory to look in for relative path images\n"
86       "-fd font/directory       Add a directory to look in for relative path fonts\n"
87       "-sd sound/directory      Add a directory to look in for relative path sounds samples\n"
88       "-td temp/directory       Directory to store temporary files\n"
89       "-v                       Verbose output\n"
90       "-no-lossy                Do NOT allow images to be lossy\n"
91       "-no-comp                 Do NOT allow images to be stored with lossless compression\n"
92       "-no-raw                  Do NOT allow images to be stored with zero compression (raw)\n"
93       "-no-save                 Do NOT store the input EDC file in the EDJ file\n"
94       "-min-quality VAL         Do NOT allow lossy images with quality < VAL (0-100)\n"
95       "-max-quality VAL         Do NOT allow lossy images with quality > VAL (0-100)\n"
96       "-Ddefine_val=to          CPP style define to define input macro definitions to the .edc source\n"
97       "-fastcomp                Use a faster compression algorithm (LZ4) (mutually exclusive with -fastdecomp)\n"
98       "-fastdecomp              Use a faster decompression algorithm (LZ4HC) (mutually exclusive with -fastcomp)\n"
99       "-threads                 Compile the edje file using multiple parallel threads (by default)\n"
100       "-nothreads               Compile the edje file using only the main loop\n"
101       ,progname);
102 }
103
104 int
105 main(int argc, char **argv)
106 {
107    int i;
108    struct stat st;
109    char rpath[PATH_MAX], rpath2[PATH_MAX];
110
111    setlocale(LC_NUMERIC, "C");
112
113    if (!eina_init())
114      return -1;
115
116    _edje_cc_log_dom = eina_log_domain_register
117      ("edje_cc", EDJE_CC_DEFAULT_LOG_COLOR);
118    if (_edje_cc_log_dom < 0)
119      {
120        EINA_LOG_ERR("Enable to create a log domain.");
121        exit(-1);
122      }
123    if (!eina_log_domain_level_check(_edje_cc_log_dom, EINA_LOG_LEVEL_WARN))
124      eina_log_domain_level_set("edje_cc", EINA_LOG_LEVEL_WARN);
125
126    progname = ecore_file_file_get(argv[0]);
127    eina_log_print_cb_set(_edje_cc_log_cb, NULL);
128
129    tmp_dir = getenv("TMPDIR");
130
131    img_dirs = eina_list_append(img_dirs, ".");
132    
133    /* add defines to epp so edc files can detect edje_cc version */
134    defines = eina_list_append(defines, mem_strdup("-DEDJE_VERSION_12=12"));
135
136    for (i = 1; i < argc; i++)
137      {
138         if (!strcmp(argv[i], "-h"))
139           {
140              main_help();
141              exit(0);
142           }
143         else if (!strcmp(argv[i], "-v"))
144           {
145              eina_log_domain_level_set("edje_cc", EINA_LOG_LEVEL_INFO);
146           }
147         else if (!strcmp(argv[i], "-no-lossy"))
148           {
149              no_lossy = 1;
150           }
151         else if (!strcmp(argv[i], "-no-comp"))
152           {
153              no_comp = 1;
154           }
155         else if (!strcmp(argv[i], "-no-raw"))
156           {
157              no_raw = 1;
158           }
159         else if (!strcmp(argv[i], "-no-save"))
160           {
161              no_save = 1;
162           }
163         else if ((!strcmp(argv[i], "-id") || !strcmp(argv[i], "--image_dir")) && (i < (argc - 1)))
164           {
165              i++;
166              img_dirs = eina_list_append(img_dirs, argv[i]);
167           }
168         else if ((!strcmp(argv[i], "-fd") || !strcmp(argv[i], "--font_dir")) && (i < (argc - 1)))
169           {
170              i++;
171              fnt_dirs = eina_list_append(fnt_dirs, argv[i]);
172           }
173         else if ((!strcmp(argv[i], "-sd") || !strcmp(argv[i], "--sound_dir")) && (i < (argc - 1)))
174           {
175              i++;
176              snd_dirs = eina_list_append(snd_dirs, argv[i]);
177           }
178         else if ((!strcmp(argv[i], "-td") || !strcmp(argv[i], "--tmp_dir")) && (i < (argc - 1)))
179           {
180              i++;
181              if (!tmp_dir)
182                tmp_dir = argv[i];
183           }
184         else if ((!strcmp(argv[i], "-min-quality")) && (i < (argc - 1)))
185           {
186              i++;
187              min_quality = atoi(argv[i]);
188              if (min_quality < 0) min_quality = 0;
189              if (min_quality > 100) min_quality = 100;
190           }
191         else if ((!strcmp(argv[i], "-max-quality")) && (i < (argc - 1)))
192           {
193              i++;
194              max_quality = atoi(argv[i]);
195              if (max_quality < 0) max_quality = 0;
196              if (max_quality > 100) max_quality = 100;
197           }
198         else if (!strcmp(argv[i], "-fastcomp"))
199           {
200              compress_mode = EET_COMPRESSION_SUPERFAST;
201           }
202         else if (!strcmp(argv[i], "-fastdecomp"))
203           {
204              compress_mode = EET_COMPRESSION_VERYFAST;
205           }
206         else if (!strcmp(argv[i], "-threads"))
207           {
208              threads = 1;
209           }
210         else if (!strcmp(argv[i], "-nothreads"))
211           {
212              threads = 0;
213           }
214         else if (!strncmp(argv[i], "-D", 2))
215           {
216              defines = eina_list_append(defines, mem_strdup(argv[i]));
217           }
218         else if ((!strcmp(argv[i], "-o")) && (i < (argc - 1)))
219           {
220              i++;
221              file_out = argv[i];
222           }
223         else if ((!strcmp(argv[i], "-w")) && (i < (argc - 1)))
224           {
225              i++;
226              watchfile = argv[i];
227              unlink(watchfile);
228           }
229         else if (!file_in)
230           file_in = argv[i];
231         else if (!file_out)
232           file_out = argv[i];
233      }
234
235    if (!file_in)
236      {
237         ERR("no input file specified.");
238         main_help();
239         exit(-1);
240      }
241
242    
243
244    pfx = eina_prefix_new(argv[0],            /* argv[0] value (optional) */
245                          main,               /* an optional symbol to check path of */
246                          "EDJE",             /* env var prefix to use (XXX_PREFIX, XXX_BIN_DIR etc. */
247                          "edje",             /* dir to add after "share" (PREFIX/share/DIRNAME) */
248                          "include/edje.inc", /* a magic file to check for in PREFIX/share/DIRNAME for success */
249                          PACKAGE_BIN_DIR,    /* package bin dir @ compile time */
250                          PACKAGE_LIB_DIR,    /* package lib dir @ compile time */
251                          PACKAGE_DATA_DIR,   /* package data dir @ compile time */
252                          PACKAGE_DATA_DIR    /* if locale needed  use LOCALE_DIR */
253                         );
254
255    /* check whether file_in exists */
256 #ifdef HAVE_REALPATH
257    if (!realpath(file_in, rpath) || stat(rpath, &st) || !S_ISREG(st.st_mode))
258 #else
259    if (stat(file_in, &st) || !S_ISREG(st.st_mode))
260 #endif
261      {
262         ERR("file not found: %s.", file_in);
263         main_help();
264         exit(-1);
265      }
266
267    if (!file_out)
268       {
269          char *suffix;
270
271          if ((suffix = strstr(file_in,".edc")) && (suffix[4] == 0))
272             {
273                file_out = strdup(file_in);
274                if (file_out)
275                   {
276                      suffix = strstr(file_out,".edc");
277                      strcpy(suffix,".edj");
278                   }
279             }
280       }
281    if (!file_out)
282      {
283         ERR("no output file specified.");
284         main_help();
285         exit(-1);
286      }
287
288 #ifdef HAVE_REALPATH
289    if (realpath(file_out, rpath2) && !strcmp (rpath, rpath2))
290 #else
291    if (!strcmp (file_in, file_out))
292 #endif
293      {
294         ERR("input file equals output file.");
295         main_help();
296         exit(-1);
297      }
298
299    using_file(file_in);
300
301    if (!edje_init())
302      exit(-1);
303
304    edje_file = mem_alloc(SZ(Edje_File));
305    edje_file->compiler = strdup("edje_cc");
306    edje_file->version = EDJE_FILE_VERSION;
307    edje_file->minor = EDJE_FILE_MINOR;
308    edje_file->feature_ver = 1; /* increment this every time we add a field
309                                 * or feature to the edje file format that
310                                 * does not load nicely as a NULL or 0 value
311                                 * and needs a special fallback initialization
312                                 */
313
314    source_edd();
315    source_fetch();
316
317    data_setup();
318    compile();
319    reorder_parts();
320    data_process_scripts();
321    data_process_lookups();
322    data_process_script_lookups();
323    data_write();
324
325    eina_prefix_free(pfx);
326    pfx = NULL;
327    
328    edje_shutdown();
329    eina_log_domain_unregister(_edje_cc_log_dom);
330    eina_shutdown();
331
332    return 0;
333 }