LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-27-2008, 09:01 AM   #1
orgcandman
Member
 
Registered: May 2002
Location: new hampshire
Distribution: Fedora, RHEL
Posts: 600

Rep: Reputation: 110Reputation: 110
Makefile question


Currently, my directory structure is:

<project-root>/Makefile
<project-root>/OSAL/
<project-root>/OSAL/Makefile
<project-root>/OSAL/BaseLib/
<project-root>/OSAL/BaseLib/Makefile
<project-root>/OSAL/BaseLib/api
<project-root>/OSAL/BaseLib/inc
<project-root>/OSAL/BaseLib/src
<project-root>/NET/
<project-root>/NET/Makefile
<project-root>/NET/SocketLib/
<project-root>/NET/SocketLib/Makefile
<project-root>/NET/SocketLib/api
<project-root>/NET/SocketLib/inc
<project-root>/NET/SocketLib/src
<project-root>/scripts/
<project-root>/scripts/common.mk
<project-root>/scripts/component.mk
<project-root>/scripts/module.mk

Where OSAL and NET are considered as modules, and SocketLib and BaseLib are considered components.

The way I've structured my makefiles is:

All makefiles include either scripts/common.mk, scripts/module.mk, or scripts/component.mk, depending on what they are (the top level makefile would include common, the NET/Makefile would include module, and OSAL/BaseLib/Makefile would include component.mk as examples).

Each Makefile in a module directory (IE: NET/Makefile or OSAL/Makefile) will specify the components it wants to compile:

COMPONENT_DIR = SocketLib \
FooLib

Each Makefile in a component directory (IE: NET/SocketLib/Makefile or OSAL/BaseLib/Makefile) will specify where its include path should be
(INCS = -I./api/ -I./inc/ -I$(NET)SocketLib/api/) and what the source directories are:
(SRCS = src/ foo-src/)

All the output files should go into the module /obj/ directory (IE: NET/obj and OSAL/obj)

How I currently have it setup:
I use for statements to loop through the component directories and another set of for statements to loop through source directories, and a last set of for statements to compile all the cpp files.

How I would like to have it setup:
Using actual targets and dependancy information. I currently can get it all the way to each source directory, but from there I can't actually get it any further. I know that there is some way I can do this, I'm just not smart enough to know the way it needs to be done.

Let me know if I'm confusing or if I should post the example makefiles I have and some code to go into the directories.

-Aaron
 
Old 02-27-2008, 09:08 AM   #2
tuxdev
Senior Member
 
Registered: Jul 2005
Distribution: Slackware
Posts: 2,012

Rep: Reputation: 115Reputation: 115
Post the Makefiles, it's hard to tell exactly what you're talking about from just a description.

Though really, you're a lot better off using a higher-level build system instead of complicated Makefiles (my favourite is cmake).
 
Old 02-27-2008, 10:27 AM   #3
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
I'd look into automake and autoconf. A little difficult to pick up at first, but it makes doing the type of thing you want to do a lot easier.
ta0kira
 
Old 02-27-2008, 12:04 PM   #4
orgcandman
Member
 
Registered: May 2002
Location: new hampshire
Distribution: Fedora, RHEL
Posts: 600

Original Poster
Rep: Reputation: 110Reputation: 110
automake and autoconf aren't what I'm looking for, since they are just a templated makefile.

The current setup looks like
scripts/common.mk
Code:
VERSION = 1
PATCHLEVEL = 0
SUBLEVEL = 0
EXTRAVERSION = -rc1
NAME = Hop To It!

MAKEFLAGS += -rR --no-print-directory

OBJ_TREE_SPECIFIER := obj/
SRC_TREE_SPECIFIER := src/
INC_TREE_SPECIFIER := inc/
API_TREE_SPECIFIER := api/
CLI_SRC_TREE_SPECIFIER := cli/src/
CLI_INC_TREE_SPECIFIER := cli/inc/

HOSTCC       = gcc
HOSTCXX      = g++
HOSTCFLAGS   = -Wall -g
HOSTCXXFLAGS = -Wall -g -fno-strict-aliasing

CC       = $(HOSTCC)
CXX      = $(HOSTCXX)
CFLAGS   = $(HOSTCFLAGS)
CXXFLAGS = $(HOSTCXXFLAGS)

SS  = $(topdir)SS/
OSAL = $(topdir)OSAL/
NET  = $(topdir)NET/

RECURSE_CLEAN     = make clean   topdir=$(topdir) -C
RECURSE_COMPILE   = make compile topdir=$(topdir) -C
RECURSE_LINK      = make link    topdir=$(topdir) -C
scripts/module.mk
Code:
include $(topdir)/scripts/common.mk

MODULE_DIR := $(shell pwd)

DIR_DEPS := $(addsuffix -d, $(COMPONENT_DIRS))

compile: pre-compile $(DIR_DEPS)

pre-compile:
        echo === Building Module $(MODULE_NAME)
        if [ ! -e $(MODULE_DIR)/obj ]; then mkdir $(MODULE_DIR)/obj; fi

%-d:
        $(RECURSE_COMPILE) $(@:-d=) MODULE_DIR=$(MODULE_DIR)

link:
        @echo === APPLICATIONS for $(MODULE_NAME)
        @for d in $(COMPONENT_DIRS); do $(RECURSE_LINK) $$d MODULE_DIR=$(MODULE_DIR); done

clean:
        @echo Cleaning module $(MODULE_NAME)
        @rm -rf *~ obj/
        @for d in $(COMPONENT_DIRS); do $(RECURSE_CLEAN) $$d MODULE_DIR=$(MODULE_DIR); done
scripts/component.mk
Code:
include $(topdir)/scripts/common.mk

SRCS_LIST = src/

RECURSIVE_SRC_BUILD = make topdir=$(topdir)

INCLUDE_LIST += -I$(CURRENT_DIRECTORY)/../api  -I$(CURRENT_DIRECTORY)/../inc

CXXFLAGS += $(INCLUDE_LIST)

SRCS_DIRS := $(addsuffix -d, $(SRCS_LIST))

CURRENT_DIRECTORY := $(shell pwd)

compile: pre-compile $(SRCS_DIRS)

pre-compile:
        @echo ========= Building $(COMPONENT_NAME)

%-d: pre-build
        @echo ========= Built [$(@:-d=)] [$<][$@][$(patsubst %.cpp,%.cpp.dxx,$(wildcard $(@:-d=)*.cpp))]

pre-build:
        @echo Determining dependancy info...

%.dxx:
        @echo [CXX] `basename $@ .cpp.dxx`.o

link:
        @if [ ! -e $(topdir)/executables ]; then mkdir $(topdir)/executables; fi
        @if [ -e Make-App ]; then echo ========= Building Application $(APP_NAME); $(CXX) $(CXXFLAGS) -o $(topdir)/executables/$(APP_NAME) $(CURRENT_DIRECTORY)/src/$(APP_NAME).cpp $(OBJ_INC) $(LD_INC); fi

clean:
        @echo Cleaning component $(COMPONENT_NAME)
        @rm -rf *~ src/*~ inc/*~ api/*~


old-compile-crap:
#       @echo ========= Building $(COMPONENT_NAME)
#       @if [ ! -e Make-App ]; then for d in $(SRCS_LIST); do (cd $$d && (for f in *.cpp; do make topdir=$(topdir) component_dir=$(CURRENT_DIRECTORY) -f $(CURRENT_DIRECTORY)/Makefile $(MODULE_DIR)/obj/$$f.dxx; done) && cd $(CURRENT_DIRECTORY)); done; fi

old-dxx-crap:
#       @cd $(component_dir)
#       @$(CXX) $(CXXFLAGS) -c -o $(MODULE_DIR)/obj/`basename $@ .cpp.dxx`.o $(CURRENT_DIRECTORY)/`basename $@ .dxx`
#       @cd $(CURRENT_DIRECTORY)
OSAL/Makefile
Code:
MODULE_NAME = OS Abstraction Layer
MODULE_LIB_NAME = libosal.so

COMPONENT_DIRS = BaseLib \
                 IOLib

include $(topdir)/scripts/module.mk
OSAL/BaseLib/Makefile
Code:
COMPONENT_NAME= Task Component

include $(topdir)/scripts/components.mk

INCLUDE_LIST += -I./api/ -I./inc/ \
                -I$(SS)CollectionLib/api
Makefile
Code:
topdir := $(shell pwd)/

include scripts/common.mk

all: compile link
        @echo Built All

compile: compile-osal compile-ss compile-net
        @echo Compiled All

compile-osal:
        @$(RECURSE_COMPILE) $(OSAL)

compile-ss:
        @$(RECURSE_COMPILE) $(SS)

compile-net:
        @$(RECURSE_COMPILE) $(NET)

link: link-osal link-ss link-net
        @echo Link All

link-osal:
        @$(RECURSE_LINK) $(OSAL)

link-ss:
        @$(RECURSE_LINK) $(SS)

link-net:
        @$(RECURSE_LINK) $(NET)

clean:
        @rm -rf *~ *.o executables/
        @$(RECURSE_CLEAN) $(SS)
        @$(RECURSE_CLEAN) $(OSAL)
        @$(RECURSE_CLEAN) $(NET)
That's how I've got it laid out. The only difficulty I'm having is making a dependancy style rather than iterative style(for-based) version of component.mk so that it will put all the obj/*.cpp.dxx as dependancies and build them.

-Aaron
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
g++ makefile question zahadumy Programming 4 03-25-2007 04:18 PM
Makefile.in question lucky6969b Programming 4 12-08-2005 03:32 AM
makefile question ashwin.tanugula Programming 2 10-26-2005 03:08 PM
Question about makefile taureanyang Programming 1 03-25-2005 11:57 PM
makefile question blackzone Programming 2 12-20-2004 07:03 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:02 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration