Solved

Unix Makefile

Posted on 2004-08-12
20
1,589 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
  • 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
 
LVL 14

Assisted Solution

by:chris_calabrese
chris_calabrese earned 50 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 200 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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
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 250 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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Unix / Linux grid computing 5 127
format the code in java 6 72
sum13 challenge 24 77
FizzBuzz challenge 9 73
Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
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.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

746 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now