TransWikia.com

Can I define functions and global variables within debian rules make file?

Unix & Linux Asked by Veverke on December 15, 2020

Can I define functions and global variables within a debian rules make file, to be used within the different override_**** sections ?

Have not had success in doing so.

For example, the following is an excerpt of one of my script files. I would like to use this function and global variables within my debian rules file as well, throughout the override sections.

# console output colors
NC='33[0m' # No Color
RED='33[1;31m'
BLUE='33[1;34m'
GREEN='33[1;32m'
YELLOW='33[1;33m'

#return code
rc=999

######################### Functions #############################
function logCommandExecution()
{
    commandName=$1
    exitCode=$2
    #echo 'command name: '${commandName}' exitCode: '${exitCode}
    if [ ${exitCode} -eq 0 ] 
    then
        printf "${GREEN}${commandName}' completed successfully${NC}n"
    else 
        printf "${RED}${commandName} failed with error code [${exitCode}]${NC}n"
        exit ${exitCode}
    fi
}

One Answer

The debian/rules file is a makefile, not a sh file.

I stuck your function into a makefile to try it out:

#!/usr/bin/make -f

# console output colors
NC='33[0m' # No Color
RED='33[1;31m'
GREEN='33[1;32m'

######################### Functions #############################
function logCommandExecution()
{
    commandName=$1
    exitCode=$2
    #echo 'command name: '${commandName}' exitCode: '${exitCode}
    if [ ${exitCode} -eq 0 ] 
    then
        printf "${GREEN}${commandName}' completed successfully${NC}n"
    else 
        printf "${RED}${commandName} failed with error code [${exitCode}]${NC}n"
        exit ${exitCode}
    fi
}


all:
        logCommandExecution Passcmd 0
        logCommandExecution Failcmd 1

Then wehen I try to execute this I get:

$ make all
makefile:14: *** missing separator.  Stop.

So the answer is not directly. However there are some ways to run shell syntax in makefiles.
This answer might help with that.

I think the easiest way is to put the function in another file, and call it from debian/rules:

makefile:

#!/usr/bin/make -f
all:
        ./logCommandExecution Passcmd 0
        ./logCommandExecution Failcmd 1

logCommandExecution:

#!/bin/sh

# console output colors
NC='33[0m' # No Color
RED='33[1;31m'
GREEN='33[1;32m'

commandName=$1
exitCode=$2
#echo 'command name: '${commandName}' exitCode: '${exitCode}
if [ ${exitCode} -eq 0 ] 
then
    printf "${GREEN}${commandName}' completed successfully${NC}n"
else 
    printf "${RED}${commandName} failed with error code [${exitCode}]${NC}n"
    exit ${exitCode}
fi

Now when I make it, I get:

$ make
./logCommandExecution Passcmd 0
Passcmd' completed successfully
./logCommandExecution Failcmd 1
Failcmd failed with error code [1]
make: *** [makefile:5: all] Error 1

Answered by Stewart on December 15, 2020

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP