Sunday, October 27, 2013

Cross platform Makefile

I needed to change my Makefile to output different target information based on the Operating System. This turned out more difficult than I thought since Windows doesn't have uname. I asked some friends, and the gave me some code snippets. To start with find the OSCLASS and OSNAME.
ifeq ($(OS),Windows_NT)
    OSCLASS = windows
    OSNAME=windows
else
    OSCLASS = unix
    UNAME_S := $(shell uname -s)
    ifeq ($(UNAME_S),Linux)
     OSNAME = linux
    endif
    ifeq ($(UNAME_S),Darwin)
        OSNAME = osx
    endif
endif
You can then do switches based upon the compiling OS. The biggest one was that I needed to output to a file. The problem with files that would write to both windows and unix is that the shell commands are different, so I need to address the differences in commands.
ifeq ($(OSCLASS),unix)
    FIXDIR = $1
    COMMANDCAT = cat
else
    FIXDIR = $(subst /,\,$1)
    COMMANDCAT = type
endif
Windows uses "type", whereas unix uses "cat". I then also fix the \ issue to go from the the unix / to the windows \ with the FIXDIR command. When I type "make test", the following will output the contents of info/chunk2.txt and append it to installer/test.inf.
test:
  $(COMMANDCAT) $(call FIXDIR,info/chunk2.txt >> installer/test.inf)
In unix, you will get: cat info/chunk2.txt >> installer/test.inf
In Windows, you get: type info\chunk2.txt >> installer\test.inf

No comments:

Post a Comment