mail-notification

Fork of Jean-Yves Lefort's mail-notification, a tray icon to notify of new mail
git clone https://code.djc.id.au/git/mail-notification/

jbsrc/lib/src/core/jb-compile-options.c (6769B) - raw

      1 /*
      2  * JB, the Jean-Yves Lefort's Build System
      3  * Copyright (C) 2008 Jean-Yves Lefort <jylefort@brutele.be>
      4  *
      5  * This program is free software; you can redistribute it and/or modify
      6  * it under the terms of the GNU General Public License as published by
      7  * the Free Software Foundation; either version 3 of the License, or
      8  * (at your option) any later version.
      9  *
     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.
     14  *
     15  * You should have received a copy of the GNU General Public License along
     16  * with this program; if not, write to the Free Software Foundation, Inc.,
     17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
     18  */
     19 
     20 #include "jb-compile-options.h"
     21 #include "jb-feature.h"
     22 #include "jb-util.h"
     23 #include "jb-variable.h"
     24 
     25 JBCompileOptions *jb_compile_options = NULL;
     26 
     27 /*
     28  * We store the data in variables so that it can be set in configure
     29  * (since variables are saved to the config file).
     30  */
     31 
     32 static char *
     33 get_variable_name (JBCompileOptions *self, const char *name)
     34 {
     35   return g_strdup_printf("_compile-options-%s-%s", self->name, name);
     36 }
     37 
     38 static void
     39 init_variable (JBCompileOptions *self, const char *name)
     40 {
     41   char *varname;
     42 
     43   varname = get_variable_name(self, name);
     44   jb_variable_set_string(varname, NULL);
     45   g_free(varname);
     46 }
     47 
     48 static void
     49 add_to_variable (JBCompileOptions *self, const char *name, const char *value)
     50 {
     51   char *varname;
     52   const char *old_value;
     53 
     54   varname = get_variable_name(self, name);
     55 
     56   old_value = jb_variable_get_string_or_null(varname);
     57   if (old_value == NULL)
     58     jb_variable_set_string(varname, value);
     59   else
     60     {
     61       char *new_value;
     62 
     63       new_value = g_strdup_printf("%s %s", old_value, value);
     64       jb_variable_set_string(varname, new_value);
     65       g_free(new_value);
     66     }
     67 
     68   g_free(varname);
     69 }
     70 
     71 static const char *
     72 get_variable (JBCompileOptions *self, const char *name)
     73 {
     74   char *varname;
     75   const char *value;
     76 
     77   varname = get_variable_name(self, name);
     78   value = jb_variable_get_string_or_null(varname);
     79   g_free(varname);
     80 
     81   return value;
     82 }
     83 
     84 JBCompileOptions *
     85 jb_compile_options_new (const char *name)
     86 {
     87   JBCompileOptions *self;
     88 
     89   g_return_val_if_fail(name != NULL, NULL);
     90 
     91   self = g_new0(JBCompileOptions, 1);
     92   self->name = g_strdup(name);
     93 
     94   init_variable(self, "cflags");
     95   init_variable(self, "cppflags");
     96   init_variable(self, "ldflags");
     97   init_variable(self, "libs");
     98   init_variable(self, "gob2flags");
     99 
    100   return self;
    101 }
    102 
    103 void
    104 jb_compile_options_add_cflags (JBCompileOptions *self, const char *cflags)
    105 {
    106   g_return_if_fail(self != NULL);
    107   g_return_if_fail(cflags != NULL);
    108 
    109   add_to_variable(self, "cflags", cflags);
    110 }
    111 
    112 const char *
    113 jb_compile_options_get_cflags (JBCompileOptions *self)
    114 {
    115   g_return_val_if_fail(self != NULL, NULL);
    116 
    117   return get_variable(self, "cflags");
    118 }
    119 
    120 void
    121 jb_compile_options_add_cppflags (JBCompileOptions *self, const char *cppflags)
    122 {
    123   g_return_if_fail(self != NULL);
    124   g_return_if_fail(cppflags != NULL);
    125 
    126   add_to_variable(self, "cppflags", cppflags);
    127 }
    128 
    129 const char *
    130 jb_compile_options_get_cppflags (JBCompileOptions *self)
    131 {
    132   GString *cppflags;
    133   GSList *l;
    134 
    135   g_return_val_if_fail(self != NULL, NULL);
    136 
    137   if (self->cppflags != NULL)
    138     return self->cppflags;
    139 
    140   cppflags = g_string_new(get_variable(self, "cppflags"));
    141 
    142   /* add variable defines if working on the global JBCompileOptions */
    143 
    144   if (self == jb_compile_options)
    145     JB_LIST_FOREACH(l, jb_variables)
    146       {
    147 	JBVariable *variable = l->data;
    148 	char *symbol;
    149 
    150 	if ((variable->flags & JB_VARIABLE_C_DEFINE) == 0)
    151 	  continue;
    152 
    153 	g_assert(variable->type == jb_variable_type_bool);
    154 
    155 	symbol = g_strdelimit(g_ascii_strup(variable->name, -1), "-", '_');
    156 	g_string_append_printf(cppflags, " -DWITH_%s=%i",
    157 			       symbol,
    158 			       g_value_get_boolean(&variable->value) ? 1 : 0);
    159 	g_free(symbol);
    160       }
    161 
    162   self->cppflags = g_string_free(cppflags, FALSE);
    163 
    164   return self->cppflags;
    165 }
    166 
    167 void
    168 jb_compile_options_add_ldflags (JBCompileOptions *self, const char *ldflags)
    169 {
    170   g_return_if_fail(self != NULL);
    171   g_return_if_fail(ldflags != NULL);
    172 
    173   add_to_variable(self, "ldflags", ldflags);
    174 }
    175 
    176 const char *
    177 jb_compile_options_get_ldflags (JBCompileOptions *self)
    178 {
    179   g_return_val_if_fail(self != NULL, NULL);
    180 
    181   return get_variable(self, "ldflags");
    182 }
    183 
    184 void
    185 jb_compile_options_add_libs (JBCompileOptions *self, const char *libs)
    186 {
    187   g_return_if_fail(self != NULL);
    188   g_return_if_fail(libs != NULL);
    189 
    190   add_to_variable(self, "libs", libs);
    191 }
    192 
    193 const char *
    194 jb_compile_options_get_libs (JBCompileOptions *self)
    195 {
    196   g_return_val_if_fail(self != NULL, NULL);
    197 
    198   return get_variable(self, "libs");
    199 }
    200 
    201 void
    202 jb_compile_options_add_gob2flags (JBCompileOptions *self, const char *gob2flags)
    203 {
    204   g_return_if_fail(self != NULL);
    205   g_return_if_fail(gob2flags != NULL);
    206   g_return_if_fail(jb_feature_is_enabled(&jb_gob2_feature));
    207 
    208   add_to_variable(self, "gob2flags", gob2flags);
    209 }
    210 
    211 const char *
    212 jb_compile_options_get_gob2flags (JBCompileOptions *self)
    213 {
    214   g_return_val_if_fail(self != NULL, NULL);
    215 
    216   return get_variable(self, "gob2flags");
    217 }
    218 
    219 void
    220 jb_compile_options_add_package (JBCompileOptions *self, const char *name)
    221 {
    222   char *value;
    223 
    224   g_return_if_fail(self != NULL);
    225   g_return_if_fail(name != NULL);
    226 
    227   value = g_strdup_printf("$%s-cflags", name);
    228   jb_compile_options_add_cflags(self, value);
    229   g_free(value);
    230 
    231   value = g_strdup_printf("$%s-cppflags", name);
    232   jb_compile_options_add_cppflags(self, value);
    233   g_free(value);
    234 
    235   value = g_strdup_printf("$%s-ldflags", name);
    236   jb_compile_options_add_ldflags(self, value);
    237   g_free(value);
    238 
    239   value = g_strdup_printf("$%s-libs", name);
    240   jb_compile_options_add_libs(self, value);
    241   g_free(value);
    242 }
    243 
    244 /* evaluates the provided values immediately */
    245 void
    246 jb_compile_options_add_string_defines (JBCompileOptions *self,
    247 				       const char *name,
    248 				       ...)
    249 {
    250   va_list args;
    251 
    252   g_return_if_fail(self != NULL);
    253 
    254   va_start(args, name);
    255 
    256   while (name != NULL)
    257     {
    258       const char *value;
    259       char *evaluated;
    260       char *c_quoted;
    261       char *shell_quoted;
    262       char *cppflags;
    263 
    264       value = va_arg(args, const char *);
    265       g_assert(value != NULL);
    266 
    267       /* expand variables since we need to quote the value */
    268       evaluated = jb_variable_expand(value, NULL);
    269 
    270       c_quoted = jb_c_quote(evaluated);
    271       g_free(evaluated);
    272 
    273       shell_quoted = g_shell_quote(c_quoted);
    274       g_free(c_quoted);
    275 
    276       cppflags = g_strdup_printf("-D%s=%s", name, shell_quoted);
    277       g_free(shell_quoted);
    278 
    279       jb_compile_options_add_cppflags(self, cppflags);
    280       g_free(cppflags);
    281 
    282       name = va_arg(args, const char *);
    283     }
    284 
    285   va_end(args);
    286 }