VirtualBox

Changeset 1727 in kBuild for trunk/src/kmk


Ignore:
Timestamp:
Sep 5, 2008 1:45:52 AM (16 years ago)
Author:
bird
Message:

kmk: Two new functions $(if-expr cond,exp-on-true,exp-on-false) and $(expr expression) that uses the expreval.c expression evaluator.

Location:
trunk/src/kmk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/kmk/function.c

    r1716 r1727  
    27242724    if (e1 - s1 != e2 - s2)
    27252725      return comp_vars_ne (o, s1, e1, s2, e2, argv[2], funcname);
    2726 l_simple_compare:
    27272726    if (!memcmp (s1, s2, e1 - s1))
    27282727      return variable_buffer_output (o, "", 0);     /* eq */
     
    30893088  return variable_buffer_output (o, "", 0);
    30903089}
    3091 #endif
     3090#endif /* CONFIG_WITH_WHICH */
     3091
     3092#ifdef CONFIG_WITH_IF_CONDITIONALS
     3093
     3094/* Evaluates the expression given in the argument using the
     3095   same evaluator as for the new 'if' statements, except now
     3096   we don't force the result into a boolean like for 'if' and
     3097   '$(if-expr ,,)'. */
     3098static char *
     3099func_expr (char *o, char **argv, const char *funcname UNUSED)
     3100{
     3101  o = expr_eval_to_string (o, argv[0]);
     3102  return o;
     3103}
     3104
     3105/* Same as '$(if ,,)' except the first argument is evaluated
     3106   using the same evaluator as for the new 'if' statements. */
     3107static char *
     3108func_if_expr (char *o, char **argv, const char *funcname UNUSED)
     3109{
     3110    int rc;
     3111    char *to_expand;
     3112
     3113    /* Evaluate the condition in argv[0] and expand the 2nd or
     3114       3rd argument according to the result. */
     3115    rc = expr_eval_if_conditionals (argv[0], NULL);
     3116    to_expand = rc == 0 ? argv[1] : argv[2];
     3117    if (*to_expand)
     3118      {
     3119        char *expansion = expand_argument (to_expand, NULL);
     3120
     3121        o = variable_buffer_output (o, expansion, strlen (expansion));
     3122
     3123        free (expansion);
     3124      }
     3125
     3126    return o;
     3127}
     3128
     3129#endif /* CONFIG_WITH_IF_CONDITIONALS */
    30923130
    30933131#ifdef CONFIG_WITH_STACK
     
    31083146  struct variable *stack_var;
    31093147  const char *stack = argv[0];
    3110   const int return_item = argv[0][sizeof("stack-pop") - 1] == '\0';
    31113148
    31123149  stack_var = lookup_variable (stack, strlen (stack) );
     
    39593996  { STRING_SIZE_TUPLE("which"),         0,  0,  1,  func_which},
    39603997#endif
     3998#ifdef CONFIG_WITH_IF_CONDITIONALS
     3999  { STRING_SIZE_TUPLE("expr"),          1,  1,  0,  func_expr},
     4000  { STRING_SIZE_TUPLE("if-expr"),       2,  3,  0,  func_if_expr},
     4001#endif
    39614002#ifdef CONFIG_WITH_STACK
    39624003  { STRING_SIZE_TUPLE("stack-push"),    2,  2,  1,  func_stack_push},
  • trunk/src/kmk/testcase-ifcond.kmk

    r1722 r1727  
    2626
    2727
    28 #DEPTH = ../..
    29 #include $(PATH_KBUILD)/header.kmk
     28DEPTH = ../..
     29include $(KBUILD_PATH)/header.kmk
    3030
    3131ifeq (1,0)
     
    223223# target()
    224224#
     225if 0
    225226trg_deps_only: foobar
    226227trg_with_cmds: foobar
     
    230231$(warning works)
    231232else
    232 $(error works)
     233$(error target)
    233234endif
    234235
    235236if target trg_deps_only
    236 $(error works)
     237$(error target)
    237238else
    238239$(warning works)
     
    240241
    241242if target foobar
    242 $(error works)
    243 else
    244 $(warning works)
    245 endif
     243$(error target)
     244else
     245$(warning works)
     246endif
     247endif
    246248
    247249
     
    255257$(warning works)
    256258else
    257 $(error works)
     259$(error )
    258260endif
    259261
     
    261263$(warning works)
    262264else
    263 $(error works)
     265$(error )
    264266endif
    265267
     
    267269$(warning works)
    268270else
    269 $(error works)
     271$(error )
    270272endif
    271273
    272274if defined (var_not_defined)
    273 $(error works)
    274 else
    275 $(warning works)
     275$(error )
     276else
     277$(warning works)
     278endif
     279
     280
     281#
     282# Quick check of $(if-expr ) and $(expr ).
     283#
     284ifeq ($(if-expr 0 || 2,42,500),42)
     285$(warning if-expr test 1 succeeded)
     286else
     287$(error )
     288endif
     289
     290ifeq ($(if-expr 5+3 == 231,42,500),42)
     291$(error )
     292else
     293$(warning if-expr test 2 succeeded)
     294endif
     295
     296ifeq ($(expr 5+3),8)
     297$(warning expr test 1 succeeded)
     298else
     299$(error expr:$(expr 5+3) expected 8)
     300endif
     301
     302ifeq ($(expr 25*25),625)
     303$(warning expr test 2 succeeded)
     304else
     305$(error expr:$(expr 25*25) expected 625)
     306endif
     307
     308ifeq ($(expr 100/3),3)
     309$(error )
     310else
     311$(warning expr test 3 succeeded)
    276312endif
    277313
Note: See TracChangeset for help on using the changeset viewer.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette