避免UE4项目VS中误改源码.h文件导致编译时间长
最近几天两次触发VS中误改UE4源码头文件,导致需要编译大量源码的情况;
再好的习惯也有不可靠的时候,还是需要可靠方案解决这个问题:
官方提供了预编译版本(即从Launcher中下载的版本),但是对于程序来说,使用源码编译会比较好;比如不能在Editor中启用Dedicated Server。
UE4引擎本身C++源码已经够庞大了,加上UHT生成的反射系统相关代码,整个引擎编译时间较长;
目前使用的电脑,是以前做Cocos2d-x时使用的,CPU为8代i5,Editor+Programs编译接近2个小时;
顺便说一下,编译源码一定要配SSD(容量大点,512G起吧),HDD在链接时会卡到你怀疑人生;
由于C++编译的特性,加上UHT的存在,如果引擎某个头文件不小心改动了,编译时间太过浪费了;
方案:
使用源码版本,建议在编译前,将Engine/Source目录 或者 Engine/Source/Runtime目录及下面的所有源码文件设置为只读模式;
基本上这两目录就差不多了,因为开发过程中常查看的头文件也就是Runtime下面的模块;
如果要做Editor的扩展,也可以把Source/Editor目录处理一下;
确实需要改动源码时,可以单个文件修改下属性,毕竟这个情况比较少。
2023-10-20号更新:
写了一个js脚本,将引擎源代码中的所有'.h', '.hpp', '.c', '.cpp', '.cs'文件设置为只读模式;
原因是单个源代码设置文件肯定不现实;整个Source目录递归设置,以后如果需要切换分支,重新执行Setup脚本的时候可能会出问题;
使用方式:
本地安装node环境,终端中执行:
node ReadOnly.js DirPath
其中DirPath,就是引擎源码仓库根目录,比如 我自己的目录就是D:\UE4\UnrealEngine;
下面是JavaScript脚本
const fs = require('node:fs'); const path = require('node:path'); const fileSuffixList = ['.h', '.hpp', '.c', '.cpp', '.cs']; function setFileReadOnly(filename) { fs.chmodSync(filename, 0o444); } function isTargetFile(filename) { let index = filename.lastIndexOf('.'); let fileSuffix = filename.slice(index); return fileSuffixList.includes(fileSuffix); } function processDirectory(dirPath) { if(fs.existsSync(dirPath)) { let list = fs.readdirSync(dirPath, {withFileTypes:true}); // console.log("---> list length:", list.length, dirPath); list.forEach((dirent) => { // console.log("---> dirent filename:", dirent.name, dirent.isDirectory()); if(dirent.isDirectory()) { let subDirPath = path.join(dirent.path, dirent.name); processDirectory(subDirPath); } else { if(isTargetFile(dirent.name)) { let subFilePath = path.join(dirent.path, dirent.name); setFileReadOnly(subFilePath); console.log("---> process file:", subFilePath); } } }); } else { console.error("---> targetPath not exist:", dirPath); } } let distPath = process.argv[2]; console.log("---> target path:", distPath); processDirectory(distPath); console.log("---> finish!");