Makefile/cmake/node-gyp中区分不同平台的方法

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/absurd/article/details/54235823

最近用QTK开发一个下载(下载到开发板)工具,同时用到了Makefile/cmake和node-gyp,而且都要针对不同平台做不同的处理。这里做个记录,以备以后有需要时查阅。

Makefile

在Makefile中,可以用OS变量判断当前系统是否是Windows,然后用uname来判断当前系统是MacOS还是其它系统。

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

all:
    @echo $(PLATFORM)

cmake

在cmake中,可以通过APPLE变量判断当前系统是否是MacOS,通过UNIX变量判断当前系统是否是UNIX,其它则认为是Windows。

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

node-gyp

在binding.gyp中,可以在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/unix.cc"]
        },
        'OS=="win"', {
          "sources": ["native/serial/src/impl/list_ports/list_ports_win.cc","native/serial/src/impl/win.cc"],
          'libraries': [
          '-lsetupapi.lib',
          '-lws2_32.lib'
        ]
        }]
      ]

猜你喜欢

转载自blog.csdn.net/absurd/article/details/54235823