Back to home page

DOS ain't dead

Forum index page

Log in | Register

Back to the forum
Board view  Mix view

Multiplatform makefile for gcc (Developers)

posted by Rugxulo Homepage, Usono, 26.03.2010, 03:56
(edited by Rugxulo on 26.03.2010, 19:39)

> Could someone give me an idea how to detect OS from makefile? I have some
> sources that I'm compiling with DJGPP, MinGW32 and Linux gcc. I need to
> link it with some OS specific libraries so I need to detect OS and use
> proper -l arguments and then just type "make" without different options.
> In C source I can simply use e.g. #ifdef __WIN32__ but make probably
> doesn't have something similar.

I suspected it did, but a quick check of make.info doesn't show anything. (Eli Z. probably knows, ask on comp.os.msdos.djgpp).

> It would need to call some other utility
> for OS detection but how this utility will run under all OSes?

"uname", most likely, although it's not always installed except on *nix and Cygwin.


ifeq ($(DJGPP),)
 UNAME=uname
else
 UNAME ?= $(foreach dir,$(subst ;, ,$(PATH)),$(wildcard $(dir)/uname$(EXE)))
 ifeq ($(strip $(UNAME)),)
  UNAME=$(wildcard uname$(EXE))
 endif
endif

ifneq ($(strip $(UNAME)),)
 empty=
 space=$(empty) $(empty)
 backslashspace=\$(space)
 backslashlparen=\(
 backslashrparen=\)
 leftparen=(
 rightparen=)
TARGET=$(shell $(UNAME) -srm)
 OS_TARGET=$(subst $(rightparen),$(backslashrparen),$(subst (leftparen),$(backslashlparen),$(subst $(space),$(backslashspace),$(TARGET))))
endif


> I can also
> got rough guess by testing specific environment variables e.g. "WINDIR".

"windir" (case sensitive!) is a good choice. "DJGPP" is another good one. Not sure about detecting Cygwin uniquely, but you get the idea. ;-)

> Or maybe grepping output of "gcc -v" (I have set proper path to compiler).
> But maybe it's bad idea and I should rather make 3 scripts maked, makew,
> makel which will call make with some args...

No, you don't have to do that. (Although I think "gcc -dumpversion" is better, heh.) At least things like CBuild can detect OS for you. Not sure about CMake, Ant, Jam, etc.

EDIT: Almost forgot about this.

%OS%="Windows_NT" (or "DRDOS", "NWDOS", "OPENDOS")
(under GNU Bash) see %MACHTYPE% (i786-pc-msdosdjgpp), %OSTYPE% (msdosdjgpp)

MS-DOS 6.x on up (but not DR-DOS):

ver | find "XP"
if errorlevel 1 echo Windows XP *NOT* detected!

---
Know your limits.h

 

Complete thread:

Back to the forum
Board view  Mix view
22049 Postings in 2034 Threads, 396 registered users, 247 users online (0 registered, 247 guests)
DOS ain't dead | Admin contact
RSS Feed
powered by my little forum