rakelib/compile.rake
changeset 0 f46260ba26b1
child 3 18c56730fabf
equal deleted inserted replaced
-1:000000000000 0:f46260ba26b1
       
     1 
       
     2 desc "Compile project"
       
     3 task :'compile' => :'compile:all'
       
     4 
       
     5 
       
     6 STX_TOP_DIR = BUILD_DIR / 'stx'
       
     7 STX_CONF_DIR = STX_TOP_DIR / 'configurations'
       
     8 STX_RULES_DIR = STX_TOP_DIR / 'rules'
       
     9 if win32?
       
    10   STC = STX_TOP_DIR / 'stc' / 'stc.exe'
       
    11   LIBRUN = STX_TOP_DIR / 'librun' / 'objmingw' / 'librun.dll'
       
    12 else
       
    13   STC = STX_TOP_DIR / 'stc' / 'stc'
       
    14   LIBRUN = STX_TOP_DIR / 'librun' / 'librun.so'
       
    15 end
       
    16 
       
    17 # Returns true iff this is machine of one of the core developers...
       
    18 def core_developer_machine?
       
    19   # JV's box: jv@..., vranyj1@...
       
    20   if (ENV['USER'] == 'jv') or (ENV['USER'] == 'vranyj1')
       
    21     return true
       
    22   end
       
    23   if (ENV['USERNAME'] == 'jv') or (ENV['USERNAME'] == 'vranyj1')
       
    24     return true
       
    25   end 
       
    26   return false
       
    27 end
       
    28 
       
    29 # Return suitable gcc for compilation. For now, GCC 5.x.x produces wrong code,
       
    30 # likely because some garbage C generated.
       
    31 def gcc()
       
    32   [ 'gcc', 'gcc-4.9', 'gcc-4.8', 'gcc-4.7', 'gcc-4' ].each do | gcc |
       
    33     begin
       
    34       if (%x[#{gcc} --version] =~ /4\.\d\.\d$/) != nil then
       
    35         return gcc
       
    36       end
       
    37     rescue
       
    38       # Do nothing...
       
    39     end
       
    40   end 
       
    41   raise Exception.new("Could not find suitable GCC, please install GCC 4.9 (or 4.8 or 4.7)")
       
    42 end
       
    43 
       
    44 def should_remove_librun_and_stc_sources
       
    45     return ! core_developer_machine?
       
    46 end
       
    47 
       
    48 def make_or_raise_error(args = '')
       
    49   cmd = "#{MAKE} #{args}"
       
    50   if not system cmd
       
    51     raise Exception.new("make failed: #{cmd}");
       
    52   end
       
    53 end
       
    54 
       
    55 def rm_rf_all_in_except(directory, exceptions = [])
       
    56   if File.directory? directory then
       
    57     Dir.foreach(directory) do | each |
       
    58       if each != '.' and each != '..' and not exceptions.include? each
       
    59         rm_rf File.join(directory, each)
       
    60       end
       
    61     end
       
    62   else
       
    63     rm_f directory
       
    64   end
       
    65 end
       
    66 
       
    67 if unix? 
       
    68   STC_BINARY_FILES = [    
       
    69     'cpu_alpha.h',
       
    70     'cpu_arm.h',
       
    71     'cpu_hppa.h',
       
    72     'cpu_i386.h',
       
    73     'cpu_x86_64.h',
       
    74     'cpu_ia64.h',
       
    75     'cpu_mc68k.h',
       
    76     'cpu_mc88k.h',
       
    77     'cpu_mips.h',
       
    78     'cpu_ns32k.h',
       
    79     'cpu_power.h',
       
    80     'cpu_s390.h',
       
    81     'cpu_sparc.h',
       
    82     'cpu_vax.h',
       
    83     'stc.h',
       
    84     'stcIntern.h',
       
    85     'stcVMdata.h',
       
    86     'stcVMoffsets.h',
       
    87     'stxAsmMacros.h',
       
    88     'stxNames.h',
       
    89     'stxOSDefs.h',
       
    90     'stxTypeMacros.h',     
       
    91     'symbols.stc.seed',
       
    92     'version.h',
       
    93     'README',
       
    94 
       
    95     #unix specific
       
    96     'stx-config.sh',
       
    97     'linuxIntern.h',
       
    98     'macIntern.h',
       
    99     'makefile',
       
   100     'Makefile',
       
   101     'Make.proto',
       
   102     'stc',
       
   103     'stc.1',    
       
   104   ]
       
   105   LIBRUN_BINARY_FILES1 = [    
       
   106     'libffi-3.2.1',
       
   107     'md5.h',
       
   108     'main.c',
       
   109     'librun.so',
       
   110     'librun.a',
       
   111     'symlist.c',
       
   112     'Make.proto',
       
   113     'Makefile',
       
   114   ]
       
   115   LIBRUN_BINARY_FILES2 = []
       
   116 elsif win32?
       
   117   STC_BINARY_FILES = [    
       
   118     'cpu_alpha.h',
       
   119     'cpu_arm.h',
       
   120     'cpu_hppa.h',
       
   121     'cpu_i386.h',
       
   122     'cpu_x86_64.h',
       
   123     'cpu_ia64.h',
       
   124     'cpu_mc68k.h',
       
   125     'cpu_mc88k.h',
       
   126     'cpu_mips.h',
       
   127     'cpu_ns32k.h',
       
   128     'cpu_power.h',
       
   129     'cpu_s390.h',
       
   130     'cpu_sparc.h',
       
   131     'cpu_vax.h',
       
   132     'stc.h',
       
   133     'stcIntern.h',
       
   134     'stcVMdata.h',
       
   135     'stcVMoffsets.h',
       
   136     'stxAsmMacros.h',
       
   137     'stxNames.h',
       
   138     'stxOSDefs.h',
       
   139     'stxTypeMacros.h', 
       
   140     'symbols.stc.seed',
       
   141     'version.h',
       
   142     'README',
       
   143 
       
   144     #windows specific
       
   145     'stx-config.bat',
       
   146     'mingwmake.bat',
       
   147     'ntIntern.h',
       
   148     'nt.h',
       
   149     'Make.proto',
       
   150     'stc.exe',
       
   151   ]  
       
   152   LIBRUN_BINARY_FILES1 = [    
       
   153     'libffi-3.2.1',
       
   154     'md5.h',
       
   155     'main.c',
       
   156     'objmingw', 
       
   157     'bc.mak',
       
   158     'mingwmake.bat',
       
   159     'buildDate.h',
       
   160     'genDate.com',
       
   161   ]
       
   162   LIBRUN_BINARY_FILES2 = [
       
   163     'librun.dll',
       
   164     'librun.lib',
       
   165   ]
       
   166 else
       
   167   raise Exception.new("Unsupported platform")
       
   168 end
       
   169 
       
   170 def cleanup_stc()  
       
   171   if should_remove_librun_and_stc_sources()
       
   172     puts "Cleaning up stc..."
       
   173     begin
       
   174       rm_rf_all_in_except(STX_TOP_DIR / 'stc', STC_BINARY_FILES)    
       
   175     rescue
       
   176       # When something goes wrong, be safe and remove whole directory
       
   177       rm_rf STX_TOP_DIR / 'stc'
       
   178     end
       
   179   end
       
   180 end
       
   181 
       
   182 def cleanup_librun()
       
   183   if should_remove_librun_and_stc_sources()
       
   184     puts "Cleaning up librun..."
       
   185     begin
       
   186       rm_rf_all_in_except(STX_TOP_DIR / 'librun', LIBRUN_BINARY_FILES1)
       
   187       if win32? then
       
   188         rm_rf_all_in_except(STX_TOP_DIR / 'librun' / 'objmingw', LIBRUN_BINARY_FILES2)
       
   189       end
       
   190     rescue
       
   191       # When something goes wrong, be safe and remove whole directory
       
   192       rm_rf STX_TOP_DIR / 'librun'
       
   193     end      
       
   194   end
       
   195 end
       
   196 
       
   197 # Setup flags for GCC (both, real GCC and MinGW)
       
   198 GCC_CFLAGS_OPT = ARCH == 'i386' ? '-O' : ''
       
   199 GCC_CFLAGS_DBG = core_developer_machine? ? '-ggdb3' : ''
       
   200 GCC_CFLAGS_PIC = win32? ? '' : '-fPIC'
       
   201 GCC_CFLAGS = "-pipe -fno-omit-frame-pointer -fno-stack-protector -fwrapv #{GCC_CFLAGS_PIC} #{GCC_CFLAGS_OPT} #{GCC_CFLAGS_DBG}"
       
   202 
       
   203 
       
   204 namespace :'compile' do
       
   205 
       
   206   task :'all' => [ :'prereq', :'pre', :'main', :'post' ]
       
   207 
       
   208   task :'pre'
       
   209   task :'post'
       
   210 
       
   211   task :'prereq' => [ :'setup' ]
       
   212 
       
   213   task :'main' => [ 
       
   214         :'config',
       
   215         :'libraries',
       
   216         :'stc',       
       
   217         :'librun',
       
   218         :'application',        
       
   219         :'changesets'
       
   220       ]
       
   221 
       
   222   task :'config' => STX_TOP_DIR / 'include'
       
   223 
       
   224   task :'changesets' do
       
   225     Rake::StX::ChangeSet.standardChangesetsDo(BUILD_DIR) { | cs |
       
   226       cs.addRakeCompileInfo()
       
   227     }
       
   228   end
       
   229 
       
   230 
       
   231   directory STX_TOP_DIR / 'include'
       
   232 
       
   233   case
       
   234   when linux?
       
   235     task :'config' => [ STX_CONF_DIR / 'vendorConf',
       
   236       STX_CONF_DIR / 'myConf' ,
       
   237       STX_RULES_DIR / 'stdRules_orig' ,
       
   238       STX_RULES_DIR / 'stdRules' ]
       
   239 
       
   240     file STX_RULES_DIR / 'stdRules_orig' do
       
   241       mv STX_RULES_DIR / 'stdRules' , STX_RULES_DIR / 'stdRules_orig'
       
   242       chdir STX_RULES_DIR do
       
   243         ln_s 'stdRules_alt_GNU', 'stdRules'
       
   244       end
       
   245     end
       
   246 
       
   247     task STX_RULES_DIR / 'stdRules' do
       
   248       if not (File.exist? STX_RULES_DIR / 'stdRules')
       
   249         chdir STX_RULES_DIR do
       
   250           ln_s 'stdRules_alt_GNU', 'stdRules'
       
   251         end
       
   252       end
       
   253     end
       
   254 
       
   255     file STX_RULES_DIR / 'stdRules_orig' do
       
   256       mv STX_RULES_DIR / 'stdRules' , STX_RULES_DIR / 'stdRules_orig'
       
   257     end
       
   258 
       
   259     file STX_RULES_DIR / 'stdRules' do
       
   260       chdir STX_RULES_DIR do
       
   261         ln_s 'stdRules_alt_GNU', 'stdRules'
       
   262       end
       
   263     end
       
   264 
       
   265     task STX_CONF_DIR / 'vendorConf' do
       
   266       cp STX_CONF_DIR / 'linux-elf' / 'COMMON' / 'defines' , STX_CONF_DIR / 'vendorConf'
       
   267     end
       
   268 
       
   269     task STX_CONF_DIR / 'myConf' do
       
   270       if amd64? 
       
   271         if ARCH == 'x86_64'
       
   272           cp STX_CONF_DIR / 'linux-elf' / 'x86_64' / 'defines' , STX_CONF_DIR / 'myConf'
       
   273         else
       
   274           cp STX_CONF_DIR / 'linux-elf' / 'amd64_mode32' / 'defines' , STX_CONF_DIR / 'myConf'
       
   275         end
       
   276       else
       
   277         cp STX_CONF_DIR / 'linux-elf' / 'opt-cs-oc' / 'defines' , STX_CONF_DIR / 'myConf'
       
   278       end
       
   279 
       
   280       File.open(STX_CONF_DIR / 'myConf', 'a') do | f |
       
   281         f.puts "CC=#{gcc()}"
       
   282 
       
   283         if defined? STCCOMMONOPT
       
   284           f.puts "STCCOMMONOPT=#{STCCOMMONOPT}"
       
   285         end
       
   286 
       
   287         f.puts "OPT=#{GCC_CFLAGS}"
       
   288         f.puts "LIBRUN_OPT=$(OPT)"
       
   289         f.puts "O_RULE=__STANDARD_O_RULE__"
       
   290         f.puts "EXTRA_LIBS=-ldl -lX11 -lXext"
       
   291         f.puts "XDEFS+=-DHAVE_FONTCONFIG -DXFT"
       
   292         f.puts "XINCLUDE+=$(shell pkg-config --cflags xft)"
       
   293         f.puts "LIB_XFT=-l:libXft.so.2 -l:libfontconfig.so.1"
       
   294         if amd64? and ARCH == 'x86_64' then
       
   295           f.puts 'MAKE_ZLIB_ARG= "CFLAGS=-fPIC -O3 -DUSE_MMAP"'
       
   296           # Hack to build FFI for 64-bit Linux builds
       
   297           f.puts 'FFI_OBJS=$(FFI_DIR)/build/src/*.o $(FFI_DIR)/build/src/x86/*.o'
       
   298           f.puts 'FFI_DIR=libffi-3.0.10rc8'
       
   299           f.puts 'OPTIONAL_HAVE_FFI_ARG=-DHAVE_FFI -I$(TOP)/librun/$(FFI_DIR)/build/include'
       
   300           f.puts 'OPTIONAL_FFI_TARGET_IN_LIBRUN=ffi'
       
   301           f.puts 'FFI_CC="$(CC) -m64 -fPIC"'
       
   302           f.puts 'FFI_LD="ld -m elf_x84_64"'
       
   303         end        
       
   304       end
       
   305     end
       
   306   when win32?
       
   307     task :'config' => [ STX_RULES_DIR / 'stdRules_bc_mingwhack.txt' ]
       
   308 
       
   309     file STX_RULES_DIR / 'stdRules_bc_mingwhack.txt' do
       
   310       File.open( STX_RULES_DIR / 'stdRules_bc', 'a') do | f |
       
   311         f.puts "!if defined(USEMINGW32) || defined(USEMINGW64)"
       
   312         f.puts "CFLAGS=#{GCC_CFLAGS} $(CFLAGS1) $(CFLAGS2) $(LOCALINCLUDES) $(CLOCAL_INCL) $(CFLAGS_LOCAL)"
       
   313         f.puts "!endif"
       
   314       end
       
   315       File.open( STX_RULES_DIR / 'stdRules_bc_mingwhack.txt', 'a') do | f |
       
   316         f.puts "stdRules_bc CFLAGS already fixed"
       
   317       end
       
   318     end
       
   319   else
       
   320     error "Unsuported platform: #{Config::CONFIG['host_os']}"
       
   321   end
       
   322 
       
   323   rule 'makefile' do | t |
       
   324     if File.exist?(File.dirname(t.name) / 'GNUmakefile')
       
   325   rm (File.dirname(t.name) / 'GNUmakefile')
       
   326     end
       
   327 
       
   328     chdir File.dirname(t.name) do
       
   329       sh "'#{STX_TOP_DIR / 'rules' / 'stmkmf'}'"
       
   330     end
       
   331   end
       
   332 
       
   333   task :stc do
       
   334     if linux? and amd64? and ARCH == 'i386'
       
   335       stx_make_flags="STC_LEXLIB=libfl/libfl_pic.a"
       
   336     else
       
   337       stx_make_flags=""
       
   338     end
       
   339 
       
   340     chdir STX_TOP_DIR / 'stc' do      
       
   341       begin
       
   342         make_or_raise_error stx_make_flags
       
   343         cleanup_stc()
       
   344       rescue Exception => e
       
   345         cleanup_stc()
       
   346         cleanup_librun()
       
   347         error "Cannot compile stx:stc: #{e.description}"
       
   348       end  
       
   349     end
       
   350   
       
   351     if not File.exist? STX_TOP_DIR / 'include' / 'stx-config.h' then
       
   352       cp STX_TOP_DIR / 'stc' / 'stx-config.h' , STX_TOP_DIR / 'include' / 'stx-config.h'
       
   353     end
       
   354 
       
   355     if not File.exist? STC
       
   356       cleanup_stc()
       
   357       cleanup_librun()
       
   358       error "Cannot compile stx:stc"
       
   359     end
       
   360   end
       
   361 
       
   362  task :librun do
       
   363     chdir STX_TOP_DIR / 'librun' do
       
   364       begin
       
   365         begin
       
   366             if win32_wine?
       
   367               if not File.exist? 'libffi' / 'build_win32' / 'objbc'
       
   368                 mkdir 'libffi' / 'build_win32' / 'objbc'
       
   369               end
       
   370             end
       
   371           make_or_raise_error
       
   372           cleanup_librun()
       
   373         rescue Exception => e
       
   374           cleanup_stc()
       
   375           cleanup_librun()
       
   376           error "Cannot compile stx:librun: #{e.description}"
       
   377         end
       
   378       end
       
   379     end
       
   380   end
       
   381 
       
   382   if unix?
       
   383     task :stc => STX_TOP_DIR / 'stc' / 'makefile'
       
   384     task :librun => STX_TOP_DIR / 'librun' / 'makefile'
       
   385   end
       
   386 
       
   387   if win32? and TOOLCHAIN == 'bcc'
       
   388     directory STX_TOP_DIR / 'lib' / 'bc'
       
   389     task :librun => STX_TOP_DIR / 'lib' / 'bc'
       
   390   end
       
   391 
       
   392   task :'libraries'
       
   393 
       
   394   if unix?
       
   395     vogl_dir = STX_TOP_DIR / 'support' / 'VGL' / 'vogl'
       
   396 
       
   397     task 'libraries' => [ vogl_dir / 'src' / 'libvogl.a' ]
       
   398 
       
   399     file vogl_dir / 'src' / 'libvogl.a' => [ vogl_dir / 'makefile'  ] do
       
   400       chdir STX_TOP_DIR / 'support' / 'VGL' / 'vogl' do
       
   401         make
       
   402       end
       
   403     end
       
   404   end
       
   405   
       
   406   if win32? and TOOLCHAIN == 'bcc'
       
   407     libbc = STX_TOP_DIR / 'lib' / 'bc'
       
   408 
       
   409     task :'libraries' => [  libbc / 'cs32i.lib' ,
       
   410             libbc / 'stxc32i.lib' ,
       
   411             libbc / 'X11omf.lib' ,
       
   412             libbc / 'Xextomf.lib' ,
       
   413             STX_TOP_DIR / 'support' / 'zlib-1.2.3' / 'objbc' / 'zlib.lib'
       
   414          ]
       
   415 
       
   416     file libbc / 'cs32i.lib' => libbc do | t |
       
   417       cp STX_TOP_DIR / 'support' / 'win32' /  'borland' / (File.basename(t.name)), libbc
       
   418     end
       
   419 
       
   420     file libbc / 'stxc32i.lib' => libbc do | t |
       
   421     cp STX_TOP_DIR / 'support' / 'win32' /  'borland' / (File.basename(t.name)), libbc
       
   422     end
       
   423 
       
   424     file libbc / 'X11omf.lib' => libbc do | t |
       
   425     cp STX_TOP_DIR / 'support' / 'win32' /  'borland' / (File.basename(t.name)), libbc
       
   426     end
       
   427 
       
   428 
       
   429     file libbc / 'Xextomf.lib' => libbc do | t |
       
   430     cp STX_TOP_DIR / 'support' / 'win32' /  'borland' / (File.basename(t.name)), libbc
       
   431     end
       
   432 
       
   433     directory libbc
       
   434 
       
   435     file STX_TOP_DIR / 'support' / 'zlib-1.2.3' / 'objbc' / 'zlib.lib' do
       
   436       chdir STX_TOP_DIR / 'support' / 'zlib-1.2.3' do
       
   437         make
       
   438       end
       
   439     end
       
   440 
       
   441   end
       
   442 
       
   443 end
       
   444 
       
   445 
       
   446 if unix?
       
   447     #task :'stx:libview2:pre' => STX_TOP_DIR / 'support' / 'libjpeg-7' / 'libjpeg.a'
       
   448 
       
   449     #file STX_TOP_DIR / 'support' / 'libjpeg-7/libjpeg.a' => STX_TOP_DIR / 'support' / 'libjpeg-7' / '.libs' / 'libjpeg.a' do
       
   450     #    cp STX_TOP_DIR / 'support' / 'libjpeg-7' / '.libs' / 'libjpeg.a',
       
   451     #        STX_TOP_DIR / 'support' / 'libjpeg-7' / 'libjpeg.a'
       
   452     #end
       
   453     #
       
   454     #file STX_TOP_DIR / 'support' / 'libjpeg-7' / '.libs' / 'libjpeg.a' do
       
   455     #    chdir STX_TOP_DIR / 'libview2' do
       
   456     #        make '../support/libjpeg-7/.libs/libjpeg.a'
       
   457     #    end
       
   458     #end
       
   459 
       
   460 end
       
   461 
       
   462 
       
   463 
       
   464