2013-11-04 16:48:17 +08:00
#!/usr/bin/python
# build_native.py
# Build native codes
import sys
import os , os . path
import shutil
2013-12-25 11:01:23 +08:00
from optparse import OptionParser
2013-11-04 16:48:17 +08:00
2014-01-18 15:27:59 +08:00
def get_num_of_cpu ( ) :
2015-01-29 08:40:19 +08:00
''' The build process can be accelerated by running multiple concurrent job processes using the -j-option.
'''
try :
import multiprocessing
return multiprocessing . cpu_count ( )
except Exception :
print " Can ' t know cpuinfo, use default 1 cpu "
return 1
2014-01-18 15:27:59 +08:00
2013-12-25 11:01:23 +08:00
def check_environment_variables_sdk ( ) :
''' Checking the environment ANDROID_SDK_ROOT, which will be used for building
'''
try :
SDK_ROOT = os . environ [ ' ANDROID_SDK_ROOT ' ]
except Exception :
print " ANDROID_SDK_ROOT not defined. Please define ANDROID_SDK_ROOT in your environment "
sys . exit ( 1 )
return SDK_ROOT
2013-11-04 16:48:17 +08:00
def check_environment_variables ( ) :
''' Checking the environment NDK_ROOT, which will be used for building
'''
try :
NDK_ROOT = os . environ [ ' NDK_ROOT ' ]
except Exception :
print " NDK_ROOT not defined. Please define NDK_ROOT in your environment "
sys . exit ( 1 )
return NDK_ROOT
def select_toolchain_version ( ) :
ndk_root = check_environment_variables ( )
2014-11-27 21:56:06 +08:00
if os . path . isdir ( os . path . join ( ndk_root , " toolchains/arm-linux-androideabi-4.9 " ) ) :
os . environ [ ' NDK_TOOLCHAIN_VERSION ' ] = ' 4.9 '
print " The Selected NDK toolchain version was 4.9 ! "
elif os . path . isdir ( os . path . join ( ndk_root , " toolchains/arm-linux-androideabi-4.8 " ) ) :
2013-11-04 16:48:17 +08:00
os . environ [ ' NDK_TOOLCHAIN_VERSION ' ] = ' 4.8 '
print " The Selected NDK toolchain version was 4.8 ! "
else :
print " Couldn ' t find the gcc toolchain. "
exit ( 1 )
2013-12-25 11:01:23 +08:00
def do_build ( cocos_root , ndk_root , app_android_root , ndk_build_param , sdk_root , android_platform , build_mode ) :
2013-11-04 16:48:17 +08:00
ndk_path = os . path . join ( ndk_root , " ndk-build " )
2014-01-18 15:27:59 +08:00
num_of_cpu = get_num_of_cpu ( )
2014-11-10 15:38:32 +08:00
app_android_root = app_android_root . replace ( ' ' , ' \\ ' )
2013-12-25 11:01:23 +08:00
if ndk_build_param == None :
2014-05-20 17:00:21 +08:00
command = ' %s -j %d -C %s NDK_DEBUG= %d ' % ( ndk_path , num_of_cpu , app_android_root , build_mode == ' debug ' )
2013-11-04 16:48:17 +08:00
else :
2014-05-20 17:00:21 +08:00
command = ' %s -j %d -C %s NDK_DEBUG= %d %s ' % ( ndk_path , num_of_cpu , app_android_root , build_mode == ' debug ' , ' ' . join ( str ( e ) for e in ndk_build_param ) )
2013-12-25 11:01:23 +08:00
if os . system ( command ) != 0 :
raise Exception ( " Build dynamic library for project [ " + app_android_root + " ] fails! " )
elif android_platform is not None :
2015-01-29 08:40:19 +08:00
sdk_tool_path = os . path . join ( sdk_root , " tools/android " )
cocoslib_path = os . path . join ( cocos_root , " cocos/platform/android/java " )
2015-02-13 05:38:07 +08:00
command = ' %s update lib-project -t %s -p %s ' % ( sdk_tool_path , android_platform , cocoslib_path )
2015-01-29 08:40:19 +08:00
if os . system ( command ) != 0 :
2015-02-13 05:38:07 +08:00
raise Exception ( " update cocos lib-project [ " + cocoslib_path + " ] fails! " )
2015-01-29 08:40:19 +08:00
command = ' %s update project -t %s -p %s -s ' % ( sdk_tool_path , android_platform , app_android_root )
if os . system ( command ) != 0 :
2015-02-13 05:38:07 +08:00
raise Exception ( " update project [ " + app_android_root + " ] fails! " )
2015-01-29 08:40:19 +08:00
buildfile_path = os . path . join ( app_android_root , " build.xml " )
command = ' ant clean %s -f %s -Dsdk.dir= %s ' % ( build_mode , buildfile_path , sdk_root )
os . system ( command )
2013-11-04 16:48:17 +08:00
def copy_files ( src , dst ) :
for item in os . listdir ( src ) :
path = os . path . join ( src , item )
# Android can not package the file that ends with ".gz"
if not item . startswith ( ' . ' ) and not item . endswith ( ' .gz ' ) and os . path . isfile ( path ) :
shutil . copy ( path , dst )
if os . path . isdir ( path ) :
new_dst = os . path . join ( dst , item )
os . mkdir ( new_dst )
copy_files ( path , new_dst )
def copy_resources ( app_android_root ) :
# remove app_android_root/assets if it exists
assets_dir = os . path . join ( app_android_root , " assets " )
if os . path . isdir ( assets_dir ) :
shutil . rmtree ( assets_dir )
# copy resources
os . mkdir ( assets_dir )
2014-09-13 17:50:02 +08:00
resources_dir = os . path . join ( app_android_root , " ../Resources " )
2013-11-04 16:48:17 +08:00
if os . path . isdir ( resources_dir ) :
copy_files ( resources_dir , assets_dir )
2013-12-25 11:01:23 +08:00
def build ( ndk_build_param , android_platform , build_mode ) :
2013-11-04 16:48:17 +08:00
ndk_root = check_environment_variables ( )
2013-12-25 11:01:23 +08:00
sdk_root = None
2013-11-04 16:48:17 +08:00
select_toolchain_version ( )
current_dir = os . path . dirname ( os . path . realpath ( __file__ ) )
2013-12-24 09:42:37 +08:00
cocos_root = os . path . join ( current_dir , " ../cocos2d " )
2013-11-04 16:48:17 +08:00
app_android_root = current_dir
copy_resources ( app_android_root )
2013-12-25 11:01:23 +08:00
if android_platform is not None :
2015-01-29 08:40:19 +08:00
sdk_root = check_environment_variables_sdk ( )
if android_platform . isdigit ( ) :
android_platform = ' android- ' + android_platform
else :
print ' please use vaild android platform '
exit ( 1 )
2013-12-25 11:01:23 +08:00
if build_mode is None :
2015-01-29 08:40:19 +08:00
build_mode = ' debug '
2013-12-25 11:01:23 +08:00
elif build_mode != ' release ' :
build_mode = ' debug '
do_build ( cocos_root , ndk_root , app_android_root , ndk_build_param , sdk_root , android_platform , build_mode )
2013-11-04 16:48:17 +08:00
# -------------- main --------------
if __name__ == ' __main__ ' :
2013-12-25 11:01:23 +08:00
parser = OptionParser ( )
2014-05-05 11:45:20 +08:00
parser . add_option ( " -n " , " --ndk " , dest = " ndk_build_param " , help = ' parameter for ndk-build ' , action = " append " )
2013-12-25 11:01:23 +08:00
parser . add_option ( " -p " , " --platform " , dest = " android_platform " ,
help = ' parameter for android-update.Without the parameter,the script just build dynamic library for project. Valid android-platform are:[10|11|12|13|14|15|16|17|18|19] ' )
parser . add_option ( " -b " , " --build " , dest = " build_mode " ,
help = ' the build mode for java project,debug[default] or release.Get more information,please refer to http://developer.android.com/tools/building/building-cmdline.html ' )
( opts , args ) = parser . parse_args ( )
2014-11-27 21:56:06 +08:00
print " We will remove this script next version,you should use cocos console to build android project. \n "
2013-12-25 11:01:23 +08:00
build ( opts . ndk_build_param , opts . android_platform , opts . build_mode )