Files
GitVer/GitVer/GitVer_nuitka.cpp
Jeff d36618d101 1、支持pyinstaller打包方式
2、将python打包其他参数都放入params中
2026-05-20 15:59:51 +08:00

196 lines
4.6 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#include "pch.h"
#include "gitver.h"
#include "gitver_cli.h"
#include "gitver_common.h"
#include "gitver_process.h"
#include "gitver_version.h"
#include "gitver_nuitka.h"
extern TCHAR g_szCurModuleDir[MAX_PATH];
const UINT DEFAULT_MAJOR_WHEN_NO_TAG_FOR_NUITKA = 1;
const UINT DEFAULT_MINOR_WHEN_NO_TAG_FOR_NUITKA = 0;
static int ParseNuitkaOptions(int argc, TCHAR* argv[], LPCTSTR& lpRepoPath, CString& strExtraArgs, BOOL& bTestMode)
{
lpRepoPath = NULL;
strExtraArgs.Empty();
bTestMode = FALSE;
for (int i = 2; i < argc; ++i)
{
CString strArg = argv[i];
if (strArg.CompareNoCase(_T("-test")) == 0)
{
bTestMode = TRUE;
continue;
}
if (strArg.GetLength() > 8 && strArg.Left(8).CompareNoCase(_T("repodir=")) == 0)
{
if (lpRepoPath != NULL)
{
_tprintf(_T("错误: repodir 参数重复: %s\n"), argv[i]);
return 23;
}
LPCTSTR lpPath = argv[i] + 8;
if (!PathIsDirectory(lpPath))
{
PrintInvalidRepoPathError(lpPath);
return 23;
}
lpRepoPath = lpPath;
continue;
}
if (strArg.GetLength() > 7 && strArg.Left(7).CompareNoCase(_T("params=")) == 0)
{
if (!strExtraArgs.IsEmpty())
{
_tprintf(_T("错误: params 参数重复。\n"));
return 3;
}
CString strVal = strArg.Mid(7);
// 去掉外层双引号(如 params="--standalone"
if (strVal.GetLength() >= 2 && strVal[0] == _T('"') && strVal[strVal.GetLength() - 1] == _T('"'))
strVal = strVal.Mid(1, strVal.GetLength() - 2);
strExtraArgs = strVal;
continue;
}
_tprintf(_T("错误: 未知的参数 %s\n"), argv[i]);
return 3;
}
if (lpRepoPath == NULL)
{
lpRepoPath = g_szCurModuleDir;
}
return 0;
}
static void RunNuitkaBuild(LPCTSTR lpStartMessage, CString& strNuitkaCmd, const CString& strProductVersion, const CString& strFileVersion, LPCTSTR lpRepoPath)
{
_tprintf(_T("%s\n"), lpStartMessage);
_tprintf(_T("ProductVersion=%s\n"), strProductVersion.GetString());
_tprintf(_T("FileVersion=%s\n"), strFileVersion.GetString());
CString strBuildResult = StartProcess(NULL, strNuitkaCmd.GetBuffer(), lpRepoPath);
strNuitkaCmd.ReleaseBuffer();
if (strBuildResult.IsEmpty())
{
_tprintf(_T("错误: Nuitka 构建失败。\n"));
}
}
static int HandleNuitkaBuildCommandCore(
int argc,
TCHAR* argv[],
int nCmdPrefixLen,
BOOL bBuildModule,
LPCTSTR lpStartMessage,
LPCTSTR lpUsageName,
int nPidErrorCode,
int nBidErrorCode,
int nTagErrorCode,
int nCommitErrorCode)
{
// 解析 pid内嵌在 argv[1] 中,如 "nuitkabuild=5"
CString strPid = CString(argv[1]).Mid(nCmdPrefixLen);
UINT nPid = 0;
if (strPid.IsEmpty() || !TryParseUInt16(strPid, nPid))
{
_tprintf(_T("错误: %s= 后的 pid 无效:%s应为 0-65535 范围的整数。\n"), lpUsageName, strPid.GetString());
return nPidErrorCode;
}
LPCTSTR lpRepoPath = NULL;
CString strExtraArgs;
BOOL bTestMode = FALSE;
int nRepoArgRet = ParseNuitkaOptions(argc, argv, lpRepoPath, strExtraArgs, bTestMode);
if (nRepoArgRet != 0)
{
return nRepoArgRet;
}
CString strProductVersion;
CString strFileVersion;
VersionBuildErrorCodes errorCodes = { nBidErrorCode, nTagErrorCode, nCommitErrorCode };
int nVersionRet = BuildVersionsFromRepo(
lpRepoPath,
nPid,
errorCodes,
strProductVersion,
strFileVersion,
DEFAULT_MAJOR_WHEN_NO_TAG_FOR_NUITKA,
DEFAULT_MINOR_WHEN_NO_TAG_FOR_NUITKA);
if (nVersionRet != 0)
{
return nVersionRet;
}
if (bTestMode)
{
int nDot1 = strProductVersion.Find(_T('.'));
int nDot2 = (nDot1 >= 0) ? strProductVersion.Find(_T('.'), nDot1 + 1) : -1;
if (nDot2 > nDot1)
{
strProductVersion = strProductVersion.Left(nDot2 + 1) + _T("0.0");
}
_tprintf(_T("[test] 测试版本号: ProductVersion=%s\n"), strProductVersion.GetString());
}
CString strNuitkaCmd;
if (bBuildModule)
{
strNuitkaCmd.Format(
_T("cmd /c python -m nuitka --module --windows-product-version=%s --windows-file-version=%s %s"),
strProductVersion.GetString(),
strFileVersion.GetString(),
strExtraArgs.GetString());
}
else
{
strNuitkaCmd.Format(
_T("cmd /c python -m nuitka --windows-product-version=%s --windows-file-version=%s %s"),
strProductVersion.GetString(),
strFileVersion.GetString(),
strExtraArgs.GetString());
}
RunNuitkaBuild(lpStartMessage, strNuitkaCmd, strProductVersion, strFileVersion, lpRepoPath);
return 0;
}
int HandleNuitkaBuildCommand(int argc, TCHAR* argv[])
{
return HandleNuitkaBuildCommandCore(
argc,
argv,
12,
FALSE,
_T("Nuitka 打包开始.."),
_T("nuitkabuild"),
18,
19,
20,
21);
}
int HandleNuitkaPydBuildCommand(int argc, TCHAR* argv[])
{
return HandleNuitkaBuildCommandCore(
argc,
argv,
15,
TRUE,
_T("Nuitka pyd 打包开始.."),
_T("nuitkapydbuild"),
25,
26,
27,
28);
}