Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Unix Makefile

Posted on 2004-08-12
20
Medium Priority
?
1,597 Views
Last Modified: 2013-12-26
How to assign value to a variable on given condition in unix Makefile. for eg
if a>10
then a =1
else
a = a+1

thanx
0
Comment
Question by:princyrajan
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 6
  • 4
  • +3
20 Comments
 
LVL 11

Expert Comment

by:avizit
ID: 11781872
0
 
LVL 11

Expert Comment

by:avizit
ID: 11782185
Though it seems you can have only the following four conditons which can be checked

1) ifeq
2) ifneq
3) ifdef and
4) ifndef

so you cannot do a check like a>10  

/abhijit/
0
 
LVL 6

Expert Comment

by:Talmash
ID: 11783293
if a>10
then a =1
else
a = a+1

use :

if ($a > 10) then
       set a = 1
else
      @ a+=1
endif

1) please take care of the spaces as exactly as I wrote .
2) at the head of the file write :
   #! /bin/csh -f
3) chmod 755 file (to enable execution)

tal
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 14

Assisted Solution

by:chris_calabrese
chris_calabrese earned 200 total points
ID: 11784317
I'm pretty sure talmash is pulling your leg here. But this really isn't the sort of thing that Make is good at, or even intended for.

It _is_ possible to set _static_ variables at the beginning of a makefile, and you can reference them from inside the shell-code action-bits like this:
  a="fubar"
  b="bozo"

  b: $(a)
        if [ -d "$(a)" ]; then mkdir "$(b)"; else touch "$(b)"; fi

But if you need anything more complex, then your options are either to not use Make at all and use a general-purpose scripting language (Perl and shell being the obvious choices), or to use a script to generate a makefile.
0
 
LVL 48

Expert Comment

by:Tintin
ID: 11788707
Can you give us some details on why you need to do this.  There may well be a better way of achieving what you want.
0
 

Author Comment

by:princyrajan
ID: 11790649
I am having a Makefile in which the library path have to be set (after checking
condition) according to the machine for eg: AIX or Solaris

thanx
0
 
LVL 11

Expert Comment

by:avizit
ID: 11790666
In that case  I think you can use the

"ifeq" or "ifneq" conditionals ..

0
 

Author Comment

by:princyrajan
ID: 11790935
VERSION      =
ifeq ($(VERSION),DEBUG)
VERSION =-g
endif
------------------
but, in this case if I assign a value for VERSION in command line (while make -ing a file) as DEBUG, the if condition's true part doesn't get executes. why?

thanx
0
 
LVL 11

Assisted Solution

by:avizit
avizit earned 800 total points
ID: 11791557
I tried the following example ,seems to work for me

hello.c
-------------
#include <stdio.h>

int main(){
     printf("hello\n");
     return 0;
}

Makefile
-------------
CC=gcc

ifeq ($(TEST),gee)
        CFLAGS=-g
else
        CFLAGS=-pg
endif

hello:hello.c
        $(CC) $(CFLAGS) -o hello hello.c
=================================


now i can give the following two make commands and the results are shown too,

$ make TEST=gee
gcc -g -o hello hello.c

$ make TEST=nogee
gcc -pg -o hello hello.c

guess thats what you wanted.

btw maybe in yuor example you can just pass the version thing on the command line and you need not have to use conditional for the makefile

as in

make VERSION=-g


/abhijit/
0
 

Author Comment

by:princyrajan
ID: 11792391
Hi,
I used, (AIX is my machine)

OSNAME=`uname`
ifeq ($(OSNAME),AIX)
..
..
endif

in my makefile, but the true part doesn't get executes.

Thanx
0
 
LVL 11

Expert Comment

by:avizit
ID: 11792444
i am not sure  if OSNAME=`uname`  is allowed within a makefile

but what you want can be achieved by


removing the line  OSNAME=`uname`  from the makefile
and putting the following in the command like

make OSNAME=`uname`


0
 
LVL 51

Accepted Solution

by:
ahoffmann earned 1000 total points
ID: 11796539
OSNAME=`uname -s`
AIX-PATH=path/AIX
SunOS-PATH=path/Solaris
VERSION=
g-VERSION=-g
here-we-go:
        @echo path=$($(OSNAME)-PATH)
        @echo opt=$($(DEBUG)-VERSION)
# end-of-Makefile

# don't forget the tabs in the target, then it can be improved inmany ways
# you can test with:
  make
  env DEBUG=g make
0
 

Author Comment

by:princyrajan
ID: 11808718
No my friend its not working for me. Give me some other eg:

Thanx
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 11808803
which part does not work?
if unshure try using -d and/or -dd option
0
 

Author Comment

by:princyrajan
ID: 11817541
"echo opt" is getting printed but not "echo path" (OSNAME).

Thanks
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 11818337
please post result verbatim (drag&drop)
but I assume that uname -s returns something different
please adapt the "AIX"-PATH part in
  AIX-PATH=path/AIX
to what uname -s returns (must be exact string literal, case-sensitiv!)
0
 

Author Comment

by:princyrajan
ID: 11818593
In my Machine "uname -s" returns AIX.
Generally 'uname' gets executed and its value get stored in variable in shell script but it is not working in Makefiles.

If I execute ur code it giives o/p as follows
o/p:
path=
opt=-g


-Thanks a lot
0
 
LVL 11

Expert Comment

by:avizit
ID: 11818703
can you just invoke make as

 make OSNAME=`uname`

0
 

Author Comment

by:princyrajan
ID: 11818902
In my project I am just going to include this makefile. So its not possible to invoke as "make OSNAME =`uname`

Thanks
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 11818939
is this AIX's bundled make ?
then please check man make or other docs how to use shell inside makefile (sorry, don't have AIX handy)
0

Featured Post

Survive A High-Traffic Event with Percona

Your application or website rely on your database to deliver information about products and services to your customers. You can’t afford to have your database lose performance, lose availability or become unresponsive – even for just a few minutes.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction: Displaying information on the statusbar.   Continuing from the third article about sudoku.   Open the project in visual studio. Status bar – let’s display the timestamp there.  We need to get the timestamp from the document s…
Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

722 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question