bash command in makefile (cygwin)

Hello,

In my make file (make 3.81), I use a combination of shell commands to automatically create the name of my build directory.

OS   := $(shell uname -s)
ARCH := $(shell uname -m)
KERN := $(shell uname -r | cut -d. -f 1,2)
BDIR := $(OS)_$(KERN).$(ARCH)

When I boot into different OSs, I build from the same shared directory. This code creates and assigns a different build dir based on whatever OS I am booted into. In Linux, the following command creates the dir if it doesn't exist.

# create build directory if it doesn't exist
archdir: ${BDIR}
${BDIR}:
    @mkdir -p ${BDIR}

This doesn't work under cygwin, and I'm not sure what the issue is. For now, I have to create the dir by hand when I boot into a windows OS.

Any ideas as to what the issue may be here?

I would also like to add the name of the linux distro, like fedora, debian, etc. Is there a shell command the will give that to me. If I do uname -s, I just get "Linux".

Thanks for the help,

LMHmedchem

Seems to work fine for me:

$ ls
Makefile
$ cat Makefile
OS   := $(shell uname -s)
ARCH := $(shell uname -m)
KERN := $(shell uname -r | cut -d. -f 1,2)
BDIR := $(OS)_$(KERN).$(ARCH)
 
archdir: ${BDIR}
${BDIR}:
        @mkdir -p ${BDIR}
$ make --version
GNU Make 3.81
Copyright (C) 2006  Free Software Foundation, Inc.
This is free software; see the source for copying conditions.
There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A
PARTICULAR PURPOSE.
This program built for i686-pc-cygwin
$ make
$ ls
CYGWIN_NT-5.1_1.7.i686  Makefile

Thanks for trying it for me. I wonder what the issue is here, I will look again and see if I just did something dumb.

Any suggestions about how to get the OS distro from bash?

LMHmedchem

For linux distro how about a script something like this (based on this article):

#!/bin/sh
# Detects which OS and if it is Linux then it will detect which Linux Distribution.
OS=`uname -s`
REV=`uname -r| cut -d. -f 1,2`
MACH=`uname -m`
 
if [ "${OS}" = "SunOS" ] ; then
        OS=Solaris
        ARCH=`uname -p`
        OSSTR="${OS} ${REV}(${ARCH} `uname -v`)"
elif [ "${OS}" = "AIX" ] ; then
        OSSTR="${OS} `oslevel` (`oslevel -r`)"
elif [ "${OS}" = "Linux" ] ; then
        KERNEL=`uname -r`
        if [ -f /etc/redhat-release ] ; then
                DIST='RedHat'
                PSUEDONAME=`cat /etc/redhat-release | sed s/.*\(// | sed s/\)//`
                REV=`cat /etc/redhat-release | sed s/.*release\ // | sed s/\ .*//`
        elif [ -f /etc/SUSE-release ] ; then
                DIST=`cat /etc/SUSE-release | tr "\n" ' '| sed s/VERSION.*//`
                REV=`cat /etc/SUSE-release | tr "\n" ' ' | sed s/.*=\ //`
        elif [ -f /etc/mandrake-release ] ; then
                DIST='Mandrake'
                PSUEDONAME=`cat /etc/mandrake-release | sed s/.*\(// | sed s/\)//`
                REV=`cat /etc/mandrake-release | sed s/.*release\ // | sed s/\ .*//`
        elif [ -f /etc/debian_version ] ; then
                DIST="Debian `cat /etc/debian_version`"
                REV=""
        fi
        if [ -f /etc/UnitedLinux-release ] ; then
                DIST="${DIST}[`cat /etc/UnitedLinux-release | tr "\n" ' ' | sed s/VERSION.*//`]"
        fi
        OSSTR="${OS} ${DIST} ${REV}(${PSUEDONAME} ${KERNEL} ${MACH})"
else
    OSSTR="${OS} ${REV} ${ARCH}"
fi
 
echo ${OSSTR} | tr ' ' '_'
1 Like

Thanks a ton, that is exactly what I was looking for.

LMHmedchem

---------- Post updated 03-21-11 at 02:08 PM ---------- Previous update was 03-20-11 at 07:21 PM ----------

Conditional syntax in make is a bit different than bash. Any suggestions on how to convert this to make compatible code? I know that you can run shell command from make, but I don't remember how. I don't think that make has extened if, elseif, elseif, etc. I think it just has if and else.

I also don't know the make equivalent of,
if [ -f /etc/redhat-release ] ; meaning how to test if a file exists. I don't know if I can do something like,

ifneq "$(shell -f /etc/redhat-release)" ''"

I'm not sure what to do here.

LMHmedchem

Yes, make isn't the tool for this sort of thing, that is why most tarballs come with a configure.sh script that determines what is needed and then genereates the Makefile required.

So I guess I'm back to manually creating my build directory names, or can I call the bash script from make and have it return something useful? Is there an easy way to execute bash commands from make using shell, or something like that?

LMHmedchem

Could you have a file that indicates directories are created (eg .osbuilddirmade) and put a rule in the Makefile like this:

createosbuilddir:  .osbuilddirmade
    @your_make_os_dir_script
    @touch .osbuilddirmade

Note your_make_os_dir_script is a shell script like the one posted above, but it creates the directory too. You could even touch the .osbuilddirmade file in the script if you like.

But if all you need is an ENV var in the makefile that has the long OS name why not do:

FULLOSNAME= $(shell your_os_name_script)
 
# create build directory if it doesn't exist
archdir: ${FULLOSNAME}
${FULLOSNAME}:
    @mkdir -p ${FULLOSNAME}

where your_os_name_script is the script posted above.