I want to know a parameter which is an indicator of the current OS. If am supporting Windows and Linux, how can I get a system parameter which differentiates the OS types. This for an OS independent makefile which runs both in Windows and Linux by checking the parameter in an 'if'.
In the past I've checked the value of the environment variable OS. This is set on Windows. For other platforms I've explicitly set it in the environment. This then lets you push platform specific settings into makefiles called ...
makefile.Windows_NT
makefile.Linux
makefile.HPUX
In my main makefile I then just do
SUPPORTED_PLATFORMS=Windows_NT AIX AIX32 Solaris8 Linux HPUX Solaris_64
ifeq (,$(findstring $(OS),$(SUPPORTED_PLATFORMS)))
all %:
@echo The OS environment variable is set to [$(OS)].
@echo Please set the OS environment variable to one of the following:
@echo $(SUPPORTED_PLATFORMS)
else
include makefile.$(OS)
all:
@$(TCLSH) makefile.tcl
@$(MAKE) -f makefile.gen
clean:
@$(TCLSH) makefile.tcl
@$(MAKE) -f makefile.gen clean
@$(RM) makefile.gen
etags:
@$(RM) TAGS
@etags *.cpp *.h TAGS
@$(MAKE) -C Core etags
@$(MAKE) -C Components etags
@$(MAKE) -C Repository etags
tags: ctags
ctags:
@ctags *.h
@$(MAKE) -C Core ctags
@$(MAKE) -C Components ctags
@$(MAKE) -C Repository ctags
lint:
@$(TCLSH) makefile.tcl
@$(MAKE) -f makefile.gen lint
depends:
@$(TCLSH) makefile.tcl
@$(MAKE) -f makefile.gen depends
endif
This all works because I can pull in the platform specific settings via makefile.$(OS)
That's the sum total of my main makefile and it compiles for seven different platforms. You could make the detection cleverer but that would reduce comprehensability.
In each makefile.WHATEVER I provide definitions of things like
#*******************************************************************************
#
# Platform specific tools
#
CC = CL
RM = rm
LINK = LINK
ETAGS = c:\emacs\bin\etags
TCLSH = tclsh83
#*******************************************************************************
#
# Platform specific CC definitions
#
INCLUDE := $(SYS_INCDIR);$(INCLUDE);$(SOURCE_ROOT_DIR)/SivTech/cpp;$(ORACLE_INCDIR);$(DB2_INCDIR);$(ODBC_INCDIR);$(MYSQL_INCDIR);$(TCL_INCDIR);$(XML_INCDIR);$(XSLT_INCDIR);$(JNI_INCLUDE);$(ACE_INCDIR);$(TAO_INCDIR);$(TAO_SERVICES_INC);$(CPPUNIT_INCDIR);$(ICU_INCDIR);$(SAP_INCDIR);$(QAS_INCDIR);$(INFA_INCDIR);$(MELISSADATA_INCDIR);$(ADDRESSDOCTOR_INCDIR)
CC_DEFS := $(CC_DEFS) -DOS_WIN_95 -D_WIN32_WINNT=0x400 -D_MBCS -DWIN32_LEAN_AND_MEAN -DWIN32 -DWIN32_EXTRA_LEAN $(CC_DEFINES)
CC_FLAGS_CMN := /c /nologo /G7 /EHsc /W3 $(CC_FLAGS$) $(CC_DEFS) $(MYFLAGS)
CC_FLAGS_DBG := $(CC_FLAGS_CMN) /Gi /MDd /Od /Zi /RTCu /RTCs /GZ
Obviously this is quite a C/C++ focus makefile but it proves that you can abstract away all of the platform specifics.
Chris
You may be interested in checking out: Pre-defined C/C++ Compiler Macros. It's a goldmine.