SoFunction
Updated on 2025-03-02

Methods for distinguishing and judging different platforms in Makefile/cmake/node-gyp

Recently, I used QTK to develop a download (download to development board) tool, and used Makefile/cmake and node-gyp at the same time, and they all had to be processed differently for different platforms. Make a record here to check it out if necessary in the future.

Makefile

In Makefile, you can use OS variables to determine whether the current system is Windows, and then use uname to determine whether the current system is MacOS or other systems.

ifeq ($(OS),Windows_NT)
 PLATFORM="Windows"
else
 ifeq ($(shell uname),Darwin)
  PLATFORM="MacOS"
 else
  PLATFORM="Unix-Like"
 endif
endif
all:
  @echo $(PLATFORM)

cmake

In cmake, you can use the APPLE variable to determine whether the current system is MacOS, and use the UNIX variable to determine whether the current system is UNIX, and others are considered Windows.

if(APPLE)
//APPLE
elseif(UNIX)
//UNIX
else()
//Windows
endif()

node-gyp

In, different platforms can be added to conditions.

   'conditions': [
    ['OS=="mac"', {
     'xcode_settings': {
      'GCC_ENABLE_CPP_EXCEPTIONS': 'YES'
     },
     "sources": ["native/serial/src/impl/list_ports/list_ports_osx.cc","native/serial/src/impl/"]
    },
    'OS=="win"', {
     "sources": ["native/serial/src/impl/list_ports/list_ports_win.cc","native/serial/src/impl/"],
     'libraries': [
     '-',
     '-lws2_32.lib'
    ]
    }]
   ]

Summarize

The above is the entire content of this article. I hope that the content of this article has certain reference value for your study or work. Thank you for your support. If you want to know more about it, please see the following links