Skip to content

Instantly share code, notes, and snippets.

@cloderic
Last active December 12, 2015 07:48
Show Gist options
  • Save cloderic/4739334 to your computer and use it in GitHub Desktop.
Save cloderic/4739334 to your computer and use it in GitHub Desktop.
cocos2d-x SDK extraction
### Cocos2dx package configuration ###
# This module configure the cocos2dx libs.
#
# WARNING: This is not supposed to be a full fledged configuration file,
# it just supports our current needs
#
# It defines the following:
# - COCOS2DX_INCLUDE_DIRS (include directories);
# - The imported target libcocos2d (shared library);
# - COCOS2DX_DLLS_RELEASE (if WIN32 - all needed release dlls);
# - COCOS2DX_DLLS_DEBUG (if WIN32 - all needed debug dlls);
cmake_minimum_required(VERSION 2.8.7)
get_filename_component(COCOS2DX_ROOT "${CMAKE_CURRENT_LIST_DIR}" ABSOLUTE)
# Include directories
list(APPEND COCOS2DX_INCLUDE_DIRS
"${COCOS2DX_ROOT}/cocos2dx"
"${COCOS2DX_ROOT}/cocos2dx/include"
"${COCOS2DX_ROOT}/cocos2dx/kazmath/include"
)
if (WIN32)
list(APPEND COCOS2DX_INCLUDE_DIRS
"${COCOS2DX_ROOT}/cocos2dx/platform/win32"
"${COCOS2DX_ROOT}/cocos2dx/platform/third_party/win32/OGLES")
endif()
add_library(libcocos2d SHARED IMPORTED)
set_target_properties(libcocos2d PROPERTIES
IMPORTED_IMPLIB "${COCOS2DX_ROOT}/Release.win32/libcocos2d.lib"
IMPORTED_LOCATION "${COCOS2DX_ROOT}/Release.win32/libcocos2d.dll"
IMPORTED_IMPLIB_DEBUG "${COCOS2DX_ROOT}/Debug.win32/libcocos2d.lib"
IMPORTED_LOCATION_DEBUG "${COCOS2DX_ROOT}/Debug.win32/libcocos2d.dll"
)
if (WIN32)
set(COCOS2DX_DLLS_RELEASE "@dlls_release")
set(COCOS2DX_DLLS_DEBUG "@dlls_debug")
endif()
macro(cocos2dx_add_copy_dlls target)
if (WIN32)
foreach(dll ${COCOS2DX_DLLS_RELEASE})
add_custom_command(
TARGET ${target}
POST_BUILD
COMMAND IF $<CONFIGURATION>==Release XCOPY /V /Y \"${dll}\" \"$<TARGET_FILE_DIR:${target}>\"
COMMENT "Copying ${dll} to target dir (if configuration is Release)")
endforeach()
foreach(dll ${COCOS2DX_DLLS_DEBUG})
add_custom_command(
TARGET ${target}
POST_BUILD
COMMAND IF $<CONFIGURATION>==Release XCOPY /V /Y \"${dll}\" \"$<TARGET_FILE_DIR:${target}>\"
COMMENT "Copying ${dll} to target dir (if configuration is Release)")
endforeach()
endif()
endmacro()
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
# Version 2, December 2004
#
# Copyright (C) 2013 Clodéric Mars <cloderic.mars@masagroup.net>
#
# Everyone is permitted to copy and distribute verbatim or modified
# copies of this license document, and changing it is allowed as long
# as the name is changed.
#
# DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
# TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
#
# 0. You just DO WHAT THE FUCK YOU WANT TO.
import os
import re
import string
import shutil
import argparse
import zipfile
class CMakeTemplate(string.Template):
delimiter = '@';
idpattern = r'[a-z][_a-z0-9]*';
class FileFilter:
def __init__(self, caption, inputDir, inputPattern, installDir):
self.caption = caption;
self.inputDir = inputDir;
self.inputPattern = re.compile(inputPattern);
self.installDir = installDir;
self.matchedFiles = [];
self.outputFiles = [];
self.installedFiles = [];
def match(self, filePath, verbose = False):
filePath = os.path.relpath(filePath,self.inputDir);
matchResult = self.inputPattern.match(filePath);
if (matchResult):
if verbose:
print "{0} is as a {1}.".format(filePath,self.caption);
self.matchedFiles.append(filePath);
self.outputFiles.append(matchResult.group(1));
return True;
else:
#if verbose:
# print "{0} is NOT a {1}.".format(filePath,self.caption);
return False;
def install(self, installDir, verbose = False, copy = True):
print "Installing {0} {1}:".format(len(self.matchedFiles),self.caption);
for i in range(0,len(self.matchedFiles)):
source = os.path.join(self.inputDir,self.matchedFiles[i]);
destination = os.path.join(self.installDir,self.outputFiles[i]);
self.installedFiles.append(destination);
destination = os.path.join(installDir,destination);
if verbose:
print "Copying {0} to {1}.".format(source,destination);
if copy:
destinationDir = os.path.dirname(destination);
if not os.access(destinationDir,os.R_OK):
os.makedirs(destinationDir);
shutil.copy(source,destination);
def main() :
defaultOutput = "out"
parser = argparse.ArgumentParser(description="Extract the the Cocos2d-x SDK")
parser.add_argument("-s","--source", help="Path to the Cocos2d-x SDK", required=True)
parser.add_argument("-o","--output", help="Path to the output directory (default is {})".format(defaultOutput), default=defaultOutput)
parser.add_argument("-z","--zip", help="Path to the output zip (leave empty to bypass the zip generation)")
parser.add_argument("-v","--verbose", help="Verbose", action="store_true")
args = parser.parse_args()
# os.path always uses '/' as file separator
dlls_vc100 = FileFilter("vc100 release dlls",args.source,r"^Release\.win32/(.*\.dll)$","Release.win32/");
dlls_vc100_d = FileFilter("vc100 debug dlls",args.source,r"^Debug\.win32/(.*\.dll)$","Debug.win32/");
filters = [
FileFilter("headers",args.source,r"^cocos2dx/(.*\.h)$","cocos2dx"),
FileFilter("vc100 debug libs",args.source,r"^Debug\.win32/(.*\.lib)$","Debug.win32/"),
FileFilter("vc100 release libs",args.source,r"^Release\.win32/(.*\.lib)$","Release.win32/"),
dlls_vc100_d,
dlls_vc100,
FileFilter("license files",args.source,r"^licenses/(.*)$","licenses/"),
];
# Do the filtering
for root, dirs, files in os.walk(args.source):
for f in files:
filePath = os.path.join(root,f);
for filter in filters:
if filter.match(filePath,args.verbose):
break;
# Copy the files
for filter in filters:
filter.install(args.output,args.verbose,True);
cmakeTemplatePath = "Cocos2dxConfig.cmake.in";
cmakeInstallationPath = os.path.join(args.output,"Cocos2dxConfig.cmake");
# Open the cmake template
f = open(cmakeTemplatePath,'r');
cmakeTemplate = CMakeTemplate(f.read());
f.close();
dlls_vc100_list = [];
for dll_vc100 in dlls_vc100.installedFiles:
dlls_vc100_list.append("${{COCOS2DX_ROOT}}/{0}".format(dll_vc100));
dlls_vc100_d_list = [];
for dll_vc100_d in dlls_vc100_d.installedFiles:
dlls_vc100_d_list.append("${{COCOS2DX_ROOT}}/{0}".format(dll_vc100_d));
# Write the formatted output
f = open(cmakeInstallationPath,'w');
f.write(cmakeTemplate.substitute(
dlls_release=";".join(dlls_vc100_list),
dlls_debug=";".join(dlls_vc100_d_list)
))
f.close()
print "'{}' generated".format(cmakeInstallationPath)
# Create the zip if needed
if args.zip:
# create the archive
archive = zipfile.ZipFile(args.zip,'w', zipfile.ZIP_DEFLATED)
# add the files to it
for path, dirs, files in os.walk(args.output):
for file in files:
filePath = os.path.join(path,file)
archive.write(filePath,os.path.relpath(filePath,args.output))
# flush the archive
archive.close()
print "Archive of the SDK generated to '{}'".format(args.zip)
if __name__ == "__main__" :
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment