Cocos2dx 项目 Android Studio 打包备忘 CMakeLists


Cocos2dx 项目 Android Studio 打包备忘 CMakeLists

  • 项目根目录 CMakeLists.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
cmake_minimum_required(VERSION 3.6)

set(APP_NAME XXXGame)

project(${APP_NAME})

set(COCOS2DX_ROOT_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cocos2d)
set(CMAKE_MODULE_PATH ${COCOS2DX_ROOT_PATH}/cmake/Modules/)

include(CocosBuildSet)
add_subdirectory(${COCOS2DX_ROOT_PATH}/cocos ${ENGINE_BINARY_PATH}/cocos/core)

# record sources, headers, resources...
set(GAME_SOURCE)
set(GAME_HEADER)

set(GAME_RES_FOLDER
"${CMAKE_CURRENT_SOURCE_DIR}/Resources"
)
if(APPLE OR WINDOWS)
cocos_mark_multi_resources(common_res_files RES_TO "Resources" FOLDERS ${GAME_RES_FOLDER})
endif()

# add cross-platforms source files and header files
# 下面为子模块的CMake编译文件路径
include(Classes/Common/CMakeLists.txt)
include(Classes/Dao/CMakeLists.txt)

# 下面为所有cpp和h文件的路径,包括子模块里的,比如${GD_COMMON_HEADER}
list(APPEND GAME_SOURCE
Classes/AppDelegate.cpp
${GD_COMMON_HEADER}
${GD_DAO_HEADER}
)
list(APPEND GAME_HEADER
Classes/AppDelegate.h
${GD_COMMON_SRC}
${GD_DAO_SRC}
)

if(ANDROID)
# change APP_NAME to the share library name for Android, it's value depend on AndroidManifest.xml
set(APP_NAME XXXGame)
list(APPEND GAME_SOURCE
Classes/main.cpp
)
elseif(LINUX)
list(APPEND GAME_SOURCE
proj.linux/main.cpp
)
elseif(WINDOWS)
list(APPEND GAME_HEADER
proj.win32/main.h
proj.win32/resource.h
)
list(APPEND GAME_SOURCE
proj.win32/main.cpp
proj.win32/game.rc
${common_res_files}
)
elseif(APPLE)
if(IOS)
list(APPEND GAME_HEADER
proj.ios_mac/ios/AppController.h
proj.ios_mac/ios/RootViewController.h
)
set(APP_UI_RES
proj.ios_mac/ios/LaunchScreen.storyboard
proj.ios_mac/ios/LaunchScreenBackground.png
proj.ios_mac/ios/Images.xcassets
)
list(APPEND GAME_SOURCE
proj.ios_mac/ios/main.m
proj.ios_mac/ios/AppController.mm
proj.ios_mac/ios/RootViewController.mm
proj.ios_mac/ios/Prefix.pch
${APP_UI_RES}
)
elseif(MACOSX)
set(APP_UI_RES
proj.ios_mac/mac/Icon.icns
proj.ios_mac/mac/Info.plist
)
list(APPEND GAME_SOURCE
proj.ios_mac/mac/main.cpp
proj.ios_mac/mac/Prefix.pch
${APP_UI_RES}
)
endif()
list(APPEND GAME_SOURCE ${common_res_files})
endif()

# mark app complie info and libs info
set(all_code_files
${GAME_HEADER}
${GAME_SOURCE}
)
if(NOT ANDROID)
add_executable(${APP_NAME} ${all_code_files})
else()
add_library(${APP_NAME} SHARED ${all_code_files})
add_subdirectory(${COCOS2DX_ROOT_PATH}/cocos/platform/android ${ENGINE_BINARY_PATH}/cocos/platform)
target_link_libraries(${APP_NAME} -Wl,--whole-archive cpp_android_spec -Wl,--no-whole-archive)
endif()

target_link_libraries(${APP_NAME} cocos2d)
# 项目所有Classes的路径都要加入
target_include_directories(${APP_NAME}
PRIVATE Classes
PRIVATE ${COCOS2DX_ROOT_PATH}/cocos/audio/include/
PRIVATE Classes/Common
PRIVATE Classes/Dao
)

# mark app resources
setup_cocos_app_config(${APP_NAME})
if(APPLE)
set_target_properties(${APP_NAME} PROPERTIES RESOURCE "${APP_UI_RES}")
if(MACOSX)
set_target_properties(${APP_NAME} PROPERTIES
MACOSX_BUNDLE_INFO_PLIST "${CMAKE_CURRENT_SOURCE_DIR}/proj.ios_mac/mac/Info.plist"
)
elseif(IOS)
cocos_pak_xcode(${APP_NAME} INFO_PLIST "iOSBundleInfo.plist.in")
set_xcode_property(${APP_NAME} ASSETCATALOG_COMPILER_APPICON_NAME "AppIcon")
set_xcode_property(${APP_NAME} DEVELOPMENT_TEAM "")
set_xcode_property(${APP_NAME} CODE_SIGN_IDENTITY "iPhone Developer")
endif()
elseif(WINDOWS)
cocos_copy_target_dll(${APP_NAME})
endif()

if(LINUX OR WINDOWS)
set(APP_RES_DIR "$<TARGET_FILE_DIR:${APP_NAME}>/Resources")
cocos_copy_target_res(${APP_NAME} COPY_TO ${APP_RES_DIR} FOLDERS ${GAME_RES_FOLDER})
endif()
  • 模块 CMakeLists.txt
1
2
3
4
5
6
7
8
9
10
set(GD_COMMON_HEADER
Classes/Common/AppMacros.h
Classes/Common/ConfigHeader.h
Classes/Common/GMConfig.h
Classes/Common/GMConstant.h
)

set(GD_COMMON_SRC
Classes/Common/GMConstant.cpp
)
  • 自动生成模块 CMake文件脚本 CMakeLists.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# -*- coding: utf-8 -*-
import os
import plistlib
import re
import getpass
import datetime
import codecs
import logging
import sys

pro_name = "XXXGame"
path = os.getcwd()
files_path = path
file_path = path
head_file_name = 'CMakeLists.txt'
file_context_str = ""
# 所有文件夹,第一个字段是次目录的级别
dirList = []
# 所有文件
allfiles = []

file_context_h=""
file_context_cpp=""

def replaceContextToFile(replace_str):
global file_context_str
file_context_str = file_context_str + replace_str
head_file = codecs.open(file_path+'/'+head_file_name,'wb',encoding='UTF-8')
head_file.write(file_context_str)
head_file.close()

if not os.path.isdir(file_path):
os.makedirs(file_path)
#print "files_path",files_path
listfile = os.listdir(files_path)


def file_name(file_dir):
for root, dirs, files in os.walk(file_dir):
#print('root_dir:', root) # 当前目录路径
#print('sub_dirs:', dirs) # 当前路径下所有子目录
#print('files:', files) # 当前路径下所有非目录子文件
for f in files:
# if(re.search('mp4$'),f):
allfiles.append(os.path.join(root, f))
#test
def get_all_file(rawdir):
for root,dirs,files in os.walk(rawdir):
for f in files:
#if(re.search('mp4$'),f):
allfiles.append(os.path.join(root,f))
for dirname in dirs:
get_all_file(os.path.join(root,dirname))
return allfiles
#
file_name(files_path)

for i,value in enumerate(allfiles):
ccb_name = value
ccb_name = ccb_name.replace(files_path, '')
ccb_name = ccb_name.replace("\\", '/')
#file_name = files_path+"/"+ccb_name
file_name = ccb_name
list = file_name.rsplit('.',1)
#print list
#print file_name
if len(list) >= 2:
file_type = list[1]
if file_type == "h":
reuslt_str = '\n'
reuslt_str += ' Classes/Common' # 新模块要对应修改
reuslt_str += ccb_name
file_context_h += reuslt_str
elif file_type == "cpp":
reuslt_str = '\n'
reuslt_str += ' Classes/Common' # 新模块要对应修改
reuslt_str += ccb_name
file_context_cpp += reuslt_str
# GD_COMMON_HEADER 为变量名,新模块要对应修改
reuslt_str = 'set(GD_COMMON_HEADER'
reuslt_str += file_context_h
reuslt_str += '\n'
reuslt_str += ' )'
reuslt_str += '\n'
reuslt_str += '\n'
# GD_COMMON_SRC 为变量名,新模块要对应修改
reuslt_str += 'set(GD_COMMON_SRC'
reuslt_str += file_context_cpp
reuslt_str += '\n'
reuslt_str += ' )'
replaceContextToFile(reuslt_str)