2

I'm writing a Ruby C Extension. I will be compiling it under Windows and OSX.

What I have been unable to work out is control where the makefile and all the rest of the generated files are placed.

My extconf.rb file got conditional statements for preparing the makefile depending on the OS - so I use one for both of them.

I would like that when I compile under OSX it is all placed in an osx folder and similary under a win folder under Windows in order to keep it all separated.

As it is now all the files are generated in the same folder as my source code.

(I am very green to C and compiling applications. sorry if I have missed something obvious.)

I could write a batch to move the files afterwards, but I find it cleaner if it could be done during generation.

Matheus Moreira
  • 17,106
  • 3
  • 68
  • 107
thomthom
  • 2,854
  • 1
  • 23
  • 53

2 Answers2

1

You could put a conditional in the makefile, before the rules. Something like:

OS := $(shell uname)
ifeq ($(OS),Darwin)
FOLDER := /some_path/osx_folder
else
FOLDER := /some_other_path/win_folder
endif

EDIT:
FOLDER is just a variable; it is to be used later in the makefile, like so:

$(FOLDER)/main: $(FOLDER)/main.o $(FOLDER)/foo.o
    blah blah link $^ together to build $@

$(FOLDER)/%.o: $(SOURCE_DIRECTORY)/%.c
    blah blah compile $< to build $@

(This is a crude example-- more elegant solutions are possible if you have a lot of files to deal with.)

Beta
  • 96,650
  • 16
  • 149
  • 150
  • Where is this documented? I've been struggling to find a good overview of extconf.rb configuration. – thomthom Mar 05 '12 at 08:52
  • @thomthom, UNIX `uname` at http://en.wikipedia.org/wiki/Uname, Make conditionals at http://www.gnu.org/software/make/manual/make.html#Conditionals, I don't know about Ruby or `extconf.rb`. – Beta Mar 05 '12 at 13:28
  • I'm a bit confused of FOLDER - is that a special directive? Or is that supposed to be used later in the MAKE file to define where the files are output? – thomthom Mar 06 '12 at 12:29
1

I looked at the source of mkfm.rb and found that if you changed the current directory the Makefile was the current one.

Dir.chdir( OUTPUT_PATH )
create_makefile( EXTENSION_NAME, SOURCE_PATH )

That created the makefile in OUTPUT_PATH. As you see, I then had to provide SOURCE_PATH to create_makefile to account for the face the Makefile wasn't in the same location as the source files.

thomthom
  • 2,854
  • 1
  • 23
  • 53