-CC := $(XCRUN) -sdk $(SDKROOT) cc
-CXX := $(XCRUN) -sdk $(SDKROOT) g++
-MIG := $(XCRUN) -sdk $(SDKROOT) mig
+ifeq ($(PLATFORM),)
+ export PLATFORM := $(shell xcodebuild -sdk $(SDKROOT) -version PlatformPath | head -1 | sed 's,^.*/\([^/]*\)\.platform$$,\1,')
+ ifeq ($(PLATFORM),)
+ export PLATFORM := MacOSX
+ endif
+endif
+
+# CC/CXX get defined by make(1) by default, so we can't check them
+# against the empty string to see if they haven't been set
+ifeq ($(origin CC),default)
+ifneq ($(findstring iPhone,$(PLATFORM)),)
+ export CC := $(shell $(XCRUN) -sdk $(SDKROOT) -find gcc-4.2)
+else
+ export CC := $(shell $(XCRUN) -sdk $(SDKROOT) -find cc)
+endif
+endif
+ifeq ($(origin CXX),default)
+ifneq ($(findstring iPhone,$(PLATFORM)),)
+ export CXX := $(shell $(XCRUN) -sdk $(SDKROOT) -find g++-4.2)
+else
+ export CXX := $(shell $(XCRUN) -sdk $(SDKROOT) -find c++)
+endif
+endif
+ifeq ($(MIG),)
+ export MIG := $(shell $(XCRUN) -sdk $(SDKROOT) -find mig)
+endif