Page 1 of 1

Makefile replacement for windoze

Posted: Fri Jun 19, 2009 5:13 am
by salil_bhagurkar
Hi!

I just made these set of batch files to compile my kernel (ViSE). The advantage of this is that you don't have to specify a list of object files like you do in a makefile. You just have to set the directories where .c/.s files should be searched. It also allows for usual configuration like CC, CFLAGS, AS, ASFLAGS etc.. This configuration is in the buildall.bat file.

buildall.bat

Code: Select all

echo off

REM CONFIGURATION

REM Directories to find C and s files to compile/assemble
set DIRS=arch . class drivers stdlib

REM Compile parameters
set CC=gcc
set CFLAGS=-fno-builtin -O0 -I include -nostdinc

REM Assemble parameters
set AS=NASM
set ASFLAGS=-f aout

REM Linker parameters
set LINKER=ld
set OBJECT_LIST_FILE=ofiles.txt
set OUTPUT_FILE=kernel.bin
set LINKER_PARAMS=-T link.txt





REM Perform compilation/assembly
for %%D in (%DIRS%) do call compile.bat %%D

REM Generate a list of output files (.o)
call genout.bat > ofiles.txt

echo Linking...
%LINKER% %LINKER_PARAMS% -o %OUTPUT_FILE% @%OBJECT_LIST_FILE%

pause
compile.bat

Code: Select all

echo Checking Directory %1

REM Compile all .c
for %%X in (%1/*.c) do (call procc.bat %1/%%X)

REM Assemble all .s
for %%X in (%1/*.s) do (call procs.bat %1/%%X)
procc.bat

Code: Select all

set rawstr=%1
set str=%rawstr:.c=%
if exist %str%.o goto compare_mtime
goto do_compile
goto end


:compare_mtime
REM mcmp is a C program that will compare modification times.
REM If output file mtime is older than input file mtime then compilation is required
REM mcmp returns 2 when above condition is satisfied.
mcmp %str%.o %1
if %ERRORLEVEL%==2 goto do_compile
goto end

:do_compile
echo Compiling file %1
%CC% %CFLAGS% -c -o %str%.o %1

:end
procs.bat

Code: Select all

REM Remove .s from the filename
set rawstr=%1
set str=%rawstr:.s=%
if exist %str%.o goto compare_mtime
goto do_assemble
goto end


:compare_mtime
mcmp %str%.o %1
if %ERRORLEVEL%==2 goto do_assemble
goto end

:do_assemble
echo Assembling %1
%AS% %ASFLAGS% -o %str%.o %1

:end
genout.bat

Code: Select all

for %%D in (%DIRS%) do call outfiles.bat %%D
outfiles.bat

Code: Select all

for %%X in (%1/*.o) do echo %1/%%X

In order to allow for a large list of object files, I use the @ofiles.txt format for linker which allows the list to be in a file (in this case, ofiles.txt).
To build, you just run the buildall.bat file and it does the necessary compilation/assembly. In order to check for the file modification time, it requires a program mcmp.exe that i wrote which stats each file and compares.

mcmp.c

Code: Select all

#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/stat.h>

/*A utility to compare modification times of files*/

int main(int nr, char *argv[])
{
	struct stat buf1, buf2;
	if(nr < 3) {
		printf("Invalid number of arguments\nUsage: %s <file1> <file2>", argv[0]);
		return 3;
	}
	stat(argv[1], &buf1);
	stat(argv[2], &buf2);
	if(buf1.st_mtime > buf2.st_mtime)
		return 1;
	else
		return 2;
}

All the above in a zipped file is attached. The set of files should be placed in the top level directory of source code. All other configurations can be done in buildall.bat.

I hope this is useful. :)

Re: Makefile replacement for windoze

Posted: Fri Jun 19, 2009 6:48 am
by Steve the Pirate
salil_bhagurkar wrote:The advantage of this is that you don't have to specify a list of object files like you do in a makefile.
I can't comment on your solution really, because I don't develop on Windows, but I just thought I'd mention for anyone interested that the Makefile wiki page shows how you can do that in a makefile:
SRCFILES := $(shell find $(PROJDIRS) -mindepth 1 -maxdepth 3 -name "*.c")

OBJFILES := $(patsubst %.c,%.o,$(SRCFILES))

Re: Makefile replacement for windoze

Posted: Fri Jun 19, 2009 7:31 am
by NickJohnson
You could just use Cygwin: as long as you're doing osdevving anyway, it won't matter which libraries are exposed to the compiler. Also, I believe Dev-C++ uses makefiles for its build system internally. Really, if you want other people to compile your project, you should use a makefile if at all possible.

Re: Makefile replacement for windoze

Posted: Fri Jun 19, 2009 7:42 am
by salil_bhagurkar
Well, I use DJGPP and did this since I did not know that you can
SRCFILES := $(shell find $(PROJDIRS) -mindepth 1 -maxdepth 3 -name "*.c")

OBJFILES := $(patsubst %.c,%.o,$(SRCFILES))
in a Makefile. So, I am going to add a makefile too that will do this. But I don't want to stop using this batch based building system, since this compiles slightly faster than make and I can customize the messages that will be displayed on the console.

Re: Makefile replacement for windoze

Posted: Fri Jun 19, 2009 7:47 am
by NickJohnson
salil_bhagurkar wrote:But I don't want to stop using this batch based building system, since this compiles slightly faster than make and I can customize the messages that will be displayed on the console.
You shouldn't be trading compatibility for speed; you can display messages using echo in makefiles:

Code: Select all

all:
    echo "compiling program"
    cc *.c -o program

Re: Makefile replacement for windoze

Posted: Fri Jun 19, 2009 8:25 am
by earlz
NickJohnson wrote:
salil_bhagurkar wrote:But I don't want to stop using this batch based building system, since this compiles slightly faster than make and I can customize the messages that will be displayed on the console.
You shouldn't be trading compatibility for speed; you can display messages using echo in makefiles:

Code: Select all

all:
    echo "compiling program"
    cc *.c -o program
or

Code: Select all

all:
   @echo "compiling program"
...
so that it doesn't show the echo command