Method: MakeMakefile#create_makefile

Defined in:
lib/mkmf.rb

#create_makefile(target, srcprefix = nil) ⇒ Object

Generates the Makefile for your extension, passing along any options and preprocessor constants that you may have generated through other methods.

The target name should correspond the name of the global function name defined within your C extension, minus the Init_. For example, if your C extension is defined as Init_foo, then your target would simply be “foo”.

If any “/” characters are present in the target name, only the last name is interpreted as the target name, and the rest are considered toplevel directory names, and the generated Makefile will be altered accordingly to follow that directory structure.

For example, if you pass “test/foo” as a target name, your extension will be installed under the “test” directory. This means that in order to load the file within a Ruby program later, that directory structure will have to be followed, e.g. require 'test/foo'.

The srcprefix should be used when your source files are not in the same directory as your build script. This will not only eliminate the need for you to manually copy the source files into the same directory as your build script, but it also sets the proper target_prefix in the generated Makefile.

Setting the target_prefix will, in turn, install the generated binary in a directory under your RbConfig::CONFIG['sitearchdir'] that mimics your local filesystem when you run make install.

For example, given the following file tree:

ext/
  extconf.rb
  test/
    foo.c

And given the following code:

create_makefile('test/foo', 'test')

That will set the target_prefix in the generated Makefile to “test”. That, in turn, will create the following file tree when installed via the make install command:

/path/to/ruby/sitearchdir/test/foo.so

It is recommended that you use this approach to generate your makefiles, instead of copying files around manually, because some third party libraries may depend on the target_prefix being set properly.

The srcprefix argument can be used to override the default source directory, i.e. the current directory. It is included as part of the VPATH and added to the list of INCFLAGS.



2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
# File 'lib/mkmf.rb', line 2384

def create_makefile(target, srcprefix = nil)
  $target = target
  libpath = $DEFLIBPATH|$LIBPATH
  message "creating Makefile\n"
  MakeMakefile.rm_f "#{CONFTEST}*"
  if CONFIG["DLEXT"] == $OBJEXT
    for lib in libs = $libs.split(' ')
      lib.sub!(/-l(.*)/, %%"lib\\1.#{$LIBEXT}"%)
    end
    $defs.push(format("-DEXTLIB='%s'", libs.join(",")))
  end

  if target.include?('/')
    target_prefix, target = File.split(target)
    target_prefix[0,0] = '/'
  else
    target_prefix = ""
  end

  srcprefix ||= "$(srcdir)/#{srcprefix}".chomp('/')
  RbConfig.expand(srcdir = srcprefix.dup)

  ext = ".#{$OBJEXT}"
  orig_srcs = Dir[File.join(srcdir, "*.{#{SRC_EXT.join(%q{,})}}")]
  if not $objs
    srcs = $srcs || orig_srcs
    $objs = []
    objs = srcs.inject(Hash.new {[]}) {|h, f|
      h.key?(o = File.basename(f, ".*") << ext) or $objs << o
      h[o] <<= f
      h
    }
    unless objs.delete_if {|b, f| f.size == 1}.empty?
      dups = objs.map {|b, f|
        "#{b[/.*\./]}{#{f.collect {|n| n[/([^.]+)\z/]}.join(',')}}"
      }
      abort "source files duplication - #{dups.join(", ")}"
    end
  else
    $objs.collect! {|o| File.basename(o, ".*") << ext} unless $OBJEXT == "o"
    srcs = $srcs || $objs.collect {|o| o.chomp(ext) << ".c"}
  end
  $srcs = srcs

  hdrs = Dir[File.join(srcdir, "*.{#{HDR_EXT.join(%q{,})}}")]

  target = nil if $objs.empty?

  if target and EXPORT_PREFIX
    if File.exist?(File.join(srcdir, target + '.def'))
      deffile = "$(srcdir)/$(TARGET).def"
      unless EXPORT_PREFIX.empty?
        makedef = %{$(RUBY) -pe "$$_.sub!(/^(?=\\w)/,'#{EXPORT_PREFIX}') unless 1../^EXPORTS$/i" #{deffile}}
      end
    else
      makedef = %{(echo EXPORTS && echo $(TARGET_ENTRY))}
    end
    if makedef
      $cleanfiles << '$(DEFFILE)'
      origdef = deffile
      deffile = "$(TARGET)-$(arch).def"
    end
  end
  origdef ||= ''

  if $extout and $INSTALLFILES
    $cleanfiles.concat($INSTALLFILES.collect {|files, dir|File.join(dir, files.delete_prefix('./'))})
    $distcleandirs.concat($INSTALLFILES.collect {|files, dir| dir})
  end

  if $extmk and $static
    $defs << "-DRUBY_EXPORT=1"
  end

  if $extmk and not $extconf_h
    create_header
  end

  libpath = libpathflag(libpath)

  dllib = target ? "$(TARGET).#{CONFIG['DLEXT']}" : ""
  staticlib = target ? "$(TARGET).#$LIBEXT" : ""
  conf = configuration(srcprefix)
  conf << "\
libpath = #{($DEFLIBPATH|$LIBPATH).join(" ")}
LIBPATH = #{libpath}
DEFFILE = #{deffile}

CLEANFILES = #{$cleanfiles.join(' ')}
DISTCLEANFILES = #{$distcleanfiles.join(' ')}
DISTCLEANDIRS = #{$distcleandirs.join(' ')}

extout = #{$extout && $extout.quote}
extout_prefix = #{$extout_prefix}
target_prefix = #{target_prefix}
LOCAL_LIBS = #{$LOCAL_LIBS}
LIBS = #{$LIBRUBYARG} #{$libs} #{$LIBS}
ORIG_SRCS = #{orig_srcs.collect(&File.method(:basename)).join(' ')}
SRCS = $(ORIG_SRCS) #{(srcs - orig_srcs).collect(&File.method(:basename)).join(' ')}
OBJS = #{$objs.join(" ")}
HDRS = #{hdrs.map{|h| '$(srcdir)/' + File.basename(h)}.join(' ')}
LOCAL_HDRS = #{$headers.join(' ')}
TARGET = #{target}
TARGET_NAME = #{target && target[/\A\w+/]}
TARGET_ENTRY = #{EXPORT_PREFIX || ''}Init_$(TARGET_NAME)
DLLIB = #{dllib}
EXTSTATIC = #{$static || ""}
STATIC_LIB = #{staticlib unless $static.nil?}
#{!$extout && defined?($installed_list) ? %[INSTALLED_LIST = #{$installed_list}\n] : ""}
TIMESTAMP_DIR = #{$extout && $extmk ? '$(extout)/.timestamp' : '.'}
" #"
  # TODO: fixme
  install_dirs.each {|d| conf << ("%-14s= %s\n" % d) if /^[[:upper:]]/ =~ d[0]}
  sodir = $extout ? '$(TARGET_SO_DIR)' : '$(RUBYARCHDIR)'
  n = '$(TARGET_SO_DIR)$(TARGET)'
  cleanobjs = ["$(OBJS)"]
  if $extmk
    %w[bc i s].each {|ex| cleanobjs << "$(OBJS:.#{$OBJEXT}=.#{ex})"}
  end
  if target
    config_string('cleanobjs') {|t| cleanobjs << t.gsub(/\$\*/, "$(TARGET)#{deffile ? '-$(arch)': ''}")}
  end
  conf << "\
TARGET_SO_DIR =#{$extout ? " $(RUBYARCHDIR)/" : ''}
TARGET_SO     = $(TARGET_SO_DIR)$(DLLIB)
CLEANLIBS     = #{'$(TARGET_SO) ' if target}#{config_string('cleanlibs') {|t| t.gsub(/\$\*/) {n}}}
CLEANOBJS     = #{cleanobjs.join(' ')} *.bak
TARGET_SO_DIR_TIMESTAMP = #{timestamp_file(sodir, target_prefix)}
" #"

  conf = yield(conf) if block_given?
  mfile = File.open("Makefile", "wb")
  mfile.puts(conf)
  mfile.print "
all:    #{$extout ? "install" : target ? "$(DLLIB)" : "Makefile"}
static: #{$extmk && !$static ? "all" : %[$(STATIC_LIB)#{$extout ? " install-rb" : ""}]}
.PHONY: all install static install-so install-rb
.PHONY: clean clean-so clean-static clean-rb
" #"
  mfile.print CLEANINGS
  fsep = config_string('BUILD_FILE_SEPARATOR') {|s| s unless s == "/"}
  if fsep
    sep = ":/=#{fsep}"
    fseprepl = proc {|s|
      s = s.gsub("/", fsep)
      s = s.gsub(/(\$\(\w+)(\))/) {$1+sep+$2}
      s.gsub(/(\$\{\w+)(\})/) {$1+sep+$2}
    }
    rsep = ":#{fsep}=/"
  else
    fseprepl = proc {|s| s}
    sep = ""
    rsep = ""
  end
  dirs = []
  mfile.print "install: install-so install-rb\n\n"
  dir = sodir.dup
  mfile.print("install-so: ")
  if target
    f = "$(DLLIB)"
    dest = "$(TARGET_SO)"
    stamp = '$(TARGET_SO_DIR_TIMESTAMP)'
    if $extout
      mfile.puts dest
      mfile.print "clean-so::\n"
      mfile.print "\t-$(Q)$(RM) #{fseprepl[dest]} #{fseprepl[stamp]}\n"
      mfile.print "\t-$(Q)$(RM_RF) #{fseprepl['$(CLEANLIBS)']}\n"
      mfile.print "\t-$(Q)$(RMDIRS) #{fseprepl[dir]}#{$ignore_error}\n"
    else
      mfile.print "#{f} #{stamp}\n"
      mfile.print "\t$(INSTALL_PROG) #{fseprepl[f]} #{dir}\n"
      if defined?($installed_list)
        mfile.print "\t@echo #{dir}/#{File.basename(f)}>>$(INSTALLED_LIST)\n"
      end
    end
    mfile.print "clean-static::\n"
    mfile.print "\t-$(Q)$(RM) $(STATIC_LIB)\n"
  else
    mfile.puts "Makefile"
  end
  mfile.print("install-rb: pre-install-rb do-install-rb install-rb-default\n")
  mfile.print("install-rb-default: pre-install-rb-default do-install-rb-default\n")
  mfile.print("pre-install-rb: Makefile\n")
  mfile.print("pre-install-rb-default: Makefile\n")
  mfile.print("do-install-rb:\n")
  mfile.print("do-install-rb-default:\n")
  for sfx, i in [["-default", [["lib/**/*.rb", "$(RUBYLIBDIR)", "lib"]]], ["", $INSTALLFILES]]
    files = install_files(mfile, i, nil, srcprefix) or next
    for dir, *files in files
      unless dirs.include?(dir)
        dirs << dir
        mfile.print "pre-install-rb#{sfx}: #{timestamp_file(dir, target_prefix)}\n"
      end
      for f in files
        dest = "#{dir}/#{File.basename(f)}"
        mfile.print("do-install-rb#{sfx}: #{dest}\n")
        mfile.print("#{dest}: #{f} #{timestamp_file(dir, target_prefix)}\n")
        mfile.print("\t$(Q) $(#{$extout ? 'COPY' : 'INSTALL_DATA'}) #{f} $(@D)\n")
        if defined?($installed_list) and !$extout
          mfile.print("\t@echo #{dest}>>$(INSTALLED_LIST)\n")
        end
        if $extout
          mfile.print("clean-rb#{sfx}::\n")
          mfile.print("\t-$(Q)$(RM) #{fseprepl[dest]}\n")
        end
      end
    end
    mfile.print "pre-install-rb#{sfx}:\n"
    if files.empty?
      mfile.print("\t@$(NULLCMD)\n")
    else
      q = "$(MAKE) -q do-install-rb#{sfx}"
      if $nmake
        mfile.print "!if \"$(Q)\" == \"@\"\n\t@#{q} || \\\n!endif\n\t"
      else
        mfile.print "\t$(Q1:0=@#{q} || )"
      end
      mfile.print "$(ECHO1:0=echo) installing#{sfx.sub(/^-/, " ")} #{target} libraries\n"
    end
    if $extout
      dirs.uniq!
      unless dirs.empty?
        mfile.print("clean-rb#{sfx}::\n")
        for dir in dirs.sort_by {|d| -d.count('/')}
          stamp = timestamp_file(dir, target_prefix)
          mfile.print("\t-$(Q)$(RM) #{fseprepl[stamp]}\n")
          mfile.print("\t-$(Q)$(RMDIRS) #{fseprepl[dir]}#{$ignore_error}\n")
        end
      end
    end
  end
  if target and !dirs.include?(sodir)
    mfile.print "$(TARGET_SO_DIR_TIMESTAMP):\n\t$(Q) $(MAKEDIRS) $(@D) #{sodir}\n\t$(Q) $(TOUCH) $@\n"
  end
  dirs.each do |d|
    t = timestamp_file(d, target_prefix)
    mfile.print "#{t}:\n\t$(Q) $(MAKEDIRS) $(@D) #{d}\n\t$(Q) $(TOUCH) $@\n"
  end

  mfile.print "\nsite-install: site-install-so site-install-rb\nsite-install-so: install-so\nsite-install-rb: install-rb\n\n  SITEINSTALL\n\n  return unless target\n\n  mfile.print \".SUFFIXES: .\#{(SRC_EXT + [$OBJEXT, $ASMEXT]).compact.join(' .')}\\n\"\n  mfile.print \"\\n\"\n\n  compile_command = \"\\n\\t$(ECHO) compiling $(<\#{rsep})\\n\\t$(Q) %s\\n\\n\"\n  command = compile_command % COMPILE_CXX\n  asm_command = compile_command.sub(/compiling/, 'translating') % ASSEMBLE_CXX\n  CXX_EXT.each do |e|\n    each_compile_rules do |rule|\n      mfile.printf(rule, e, $OBJEXT)\n      mfile.print(command)\n      mfile.printf(rule, e, $ASMEXT)\n      mfile.print(asm_command)\n    end\n  end\n  command = compile_command % COMPILE_C\n  asm_command = compile_command.sub(/compiling/, 'translating') % ASSEMBLE_C\n  C_EXT.each do |e|\n    each_compile_rules do |rule|\n      mfile.printf(rule, e, $OBJEXT)\n      mfile.print(command)\n      mfile.printf(rule, e, $ASMEXT)\n      mfile.print(asm_command)\n    end\n  end\n\n  mfile.print \"$(TARGET_SO): \"\n  mfile.print \"$(DEFFILE) \" if makedef\n  mfile.print \"$(OBJS) Makefile\"\n  mfile.print \" $(TARGET_SO_DIR_TIMESTAMP)\" if $extout\n  mfile.print \"\\n\"\n  mfile.print \"\\t$(ECHO) linking shared-object \#{target_prefix.sub(/\\A\\/(.*)/, '\\1/')}$(DLLIB)\\n\"\n  mfile.print \"\\t-$(Q)$(RM) $(@\#{sep})\\n\"\n  link_so = LINK_SO.gsub(/^/, \"\\t$(Q) \")\n  if srcs.any?(&%r\"\\.(?:\#{CXX_EXT.join('|')})\\z\".method(:===))\n    link_so = link_so.sub(/\\bLDSHARED\\b/, '\\&XX')\n  end\n  mfile.print link_so, \"\\n\\n\"\n  unless $static.nil?\n    mfile.print \"$(STATIC_LIB): $(OBJS)\\n\\t-$(Q)$(RM) $(@\#{sep})\\n\\t\"\n    mfile.print \"$(ECHO) linking static-library $(@\#{rsep})\\n\\t$(Q) \"\n    mfile.print \"$(AR) \#{config_string('ARFLAGS') || 'cru '}$@ $(OBJS)\"\n    config_string('RANLIB') do |ranlib|\n      mfile.print \"\\n\\t-$(Q)\#{ranlib} $(@)\#{$ignore_error}\"\n    end\n  end\n  mfile.print \"\\n\\n\"\n  if makedef\n    mfile.print \"$(DEFFILE): \#{origdef}\\n\"\n    mfile.print \"\\t$(ECHO) generating $(@\#{rsep})\\n\"\n    mfile.print \"\\t$(Q) \#{makedef} > $@\\n\\n\"\n  end\n\n  depend = File.join(srcdir, \"depend\")\n  if File.exist?(depend)\n    mfile.print(\"###\\n\", *depend_rules(File.read(depend)))\n  else\n    mfile.print \"$(OBJS): $(HDRS) $(ruby_headers)\\n\"\n  end\n\n  $makefile_created = true\nensure\n  mfile.close if mfile\nend\n"