简体   繁体   English

如何根据 GitLab CI 构建脚本中的设置更改代码中的某些内容

[英]How to change something in the code according to settings in the GitLab CI build script

My code is written in C++我的代码是用 C++ 编写的
GitLab CI Compiler: MSVC2017_x64 GitLab CI 编译器:MSVC2017_x64

My project is being compiled with a GitLab pipeline on a Windows server.我的项目正在 Windows 服务器上使用 GitLab 管道编译。 I want to be able to compile parts of this project two times and somehow change something in the code, before it is compiled so that I will have two versions of the same application but with different predefined settings.我希望能够两次编译该项目的一部分,并在编译之前以某种方式更改代码中的某些内容,以便我将拥有相同应用程序的两个版本,但具有不同的预定义设置。
Something simple like the compiler setting a #define in a header or cpp file would be great.像编译器在 header 或 cpp 文件中设置#define这样简单的东西会很棒。
Using this same technique, I'd also like to hard wire the build number (pipeline ID) into the application.使用相同的技术,我还想将内部版本号(管道 ID)硬连接到应用程序中。

I've already tried the /D or -D parameter but it doesn't set a #define .我已经尝试过/D-D参数,但它没有设置#define

The solution needs to work no matter what the user's setup is so something like environment variables won't work.无论用户的设置如何,该解决方案都需要工作,因此环境变量之类的东西将不起作用。

This is my .gitlab-ci.yml file (the application is ultimately built with the qmake and nmake calls):这是我的.gitlab-ci.yml文件(应用程序最终是使用qmakenmake调用构建的):

stages:
  - build
  - test
variables:
  GIT_SUBMODULE_STRATEGY: recursive
build:
  stage: build
  only:
    - master
  tags:
    - windows
    - qt
  script:
    - call "C:\Program Files (x86)\Microsoft Visual Studio\2017\BuildTools\VC\Auxiliary\Build\vcvars64.bat"
    - call "C:\Qt\5.9.1\msvc2017_64\bin\qtenv2.bat"
    - set CL=/MP16
    - set PATH="C:\Program Files (x86)\Inno Setup 5";%PATH%
    - cd %CI_PROJECT_DIR%\(...)\Application
    - qmake CONFIG+=release
    - nmake
    - cd %CI_PROJECT_DIR%\(...)\3rdParty\Windows
    - copy (...)\Application.exe .
    - copy (...)\Application.ico .
    - windeployqt .
    - iscc InnoScript.iss

  artifacts:
    name: "%CI_COMMIT_REF_NAME%"
    expire_in: 1 month
    paths:
    - (...)\3rdParty\Windows\Application.exe

And this is how I would want it to be (with the -D mode=xy 's):这就是我想要的样子(使用-D mode=xy的):

stages:
  - build
  - test
variables:
  GIT_SUBMODULE_STRATEGY: recursive
build:
  stage: build
  only:
    - master
  tags:
    - windows
    - qt
  script:
    - call "C:\Program Files (x86)\Microsoft Visual Studio\2017\BuildTools\VC\Auxiliary\Build\vcvars64.bat"
    - call "C:\Qt\5.9.1\msvc2017_64\bin\qtenv2.bat"
    - set CL=/MP16
    - set PATH="C:\Program Files (x86)\Inno Setup 5";%PATH%
    - cd %CI_PROJECT_DIR%\(...)\Application
    - qmake CONFIG+=release
    - nmake -D mode=prod
    - qmake CONFIG+=release
    - nmake -D mode=stage
    - cd %CI_PROJECT_DIR%\(...)\3rdParty\Windows
    - copy (...)\Application.exe .
    - copy (...)\Application.ico .
    - windeployqt .
    - iscc InnoScript.iss

  artifacts:
    name: "%CI_COMMIT_REF_NAME%"
    expire_in: 1 month
    paths:
    - (...)\3rdParty\Windows\Application.exe

How do I modify my code by only using the GitLab CI or is there another, better way?如何仅使用 GitLab CI 修改我的代码,还是有其他更好的方法?

I've just found out how to do it.我刚刚发现如何做到这一点。

Before, I was trying to add the -D argument to the nmake call when instead, I needed to add a DEFINES+=MY_VAR='foo bar' to the qmake call.之前,我试图将-D参数添加到nmake调用,而我需要将DEFINES+=MY_VAR='foo bar'添加到qmake调用。
After doing this and deleting the build- folder to get rid of the outdated Makefile.Debug and Makefile.Release files I got it to work correctly.执行此操作并删除build-文件夹以摆脱过时的Makefile.DebugMakefile.Release文件后,我让它正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM