4 # This file is part of BeRTOS.
6 # Bertos is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2 of the License, or
9 # (at your option) any later version.
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 # As a special exception, you may use this file as part of a free software
21 # library without restriction. Specifically, if other files instantiate
22 # templates or use macros or inline functions from this file, or you compile
23 # this file and link it with other files to produce an executable, this
24 # file does not by itself cause the resulting executable to be covered by
25 # the GNU General Public License. This exception does not however
26 # invalidate any other reasons why the executable file might be covered by
27 # the GNU General Public License.
29 # Copyright 2010 Develer S.r.l. (http://www.develer.com/)
31 # Author: Francesco Sacchi <batt@develer.com>
43 if os.path.isdir(path):
44 shutil.rmtree(path, ignore_errors=True)
51 def findPath(start, target):
54 pa = os.path.abspath(pa + "/..")
55 if os.path.exists(pa + "/" + target):
56 return os.path.relpath(pa, start)
59 print "Usage: %s <project_dir> <preset_dir>" % sys.argv[0]
63 preset_dir = sys.argv[2]
65 if not os.path.exists(prj_dir + "/project.bertos"):
66 print "%s does not seem a Wizard created project." % prj_dir
69 p = open(prj_dir + "/project.bertos")
71 if s["WIZARD_VERSION"] < 3:
72 print "Project version too old."
74 pname = s["PROJECT_NAME"]
77 #find hw/ path for the board
78 hw_path = findPath(preset_dir, "hw")
80 print "hw/ path not found in parent directories of %s" % preset_dir
84 bertos_path = findPath(preset_dir, "bertos")
86 print "bertos/ path not found in parent directories of %s" % preset_dir
89 #Copy the project and remove unneeded files.
90 shutil.copytree(prj_dir, preset_dir)
91 remove(preset_dir + "/bertos")
92 remove(preset_dir + "/images")
93 remove(preset_dir + "/obj")
94 remove(preset_dir + "/Makefile")
95 remove(preset_dir + "/buildrev.h")
96 remove(preset_dir + "/VERSION")
97 remove(preset_dir + "/" + pname + ".project")
98 remove(preset_dir + "/" + pname + ".workspace")
100 #Flatten project sources.
101 #A wizard created project called pname
102 #usually has the following structure:
114 #This has been done in order to have the chance to
115 #add multiple projects sharing the same bertos version.
116 #After the copy and after removing the bertos tree inside the
117 #project, the double pname directory is redundant, so we move
118 #all pname sources into the parent directory
119 l = glob.glob(preset_dir + "/" + pname + "/*")
121 shutil.move(f, preset_dir)
123 #Remove the now empty project src dir and the hw/ dir.
124 #hw files are located in parent directories and are common
125 #for all projects on the same board.
126 remove(preset_dir + "/" + pname)
127 remove(preset_dir + "/hw")
129 assert(os.path.exists(preset_dir + "/" + hw_path + "/hw"))
130 assert(os.path.exists(preset_dir + "/" + bertos_path + "/bertos"))
132 #Update wizard project info.
133 #A preset is still a Wizard-editable project
134 #but has its bertos/ and hw/ dirs shared with
136 s["BERTOS_PATH"] = bertos_path
137 s["PROJECT_HW_PATH"] = hw_path
138 s["PROJECT_SRC_PATH"] = "."
141 # Calculate relative paths useful to discover where are the sources (and the hw
142 # files) relatively to the BeRTOS Makefile
143 bertos_path = os.path.abspath(preset_dir + "/" + bertos_path)
144 hw_path = os.path.abspath(preset_dir + "/" + hw_path)
146 src_path = os.path.relpath(preset_dir, bertos_path)
147 hw_path = os.path.relpath(hw_path, bertos_path)
149 # Src path and hw path relatively to the BeRTOS Makefile.
150 s["PROJECT_SRC_PATH_FROM_MAKEFILE"] = src_path
151 s["PROJECT_HW_PATH_FROM_MAKEFILE"] = hw_path
152 toolchain = s["TOOLCHAIN"]["path"]
153 s["TOOLCHAIN"]["path"] = os.path.basename(toolchain)
156 p = open(preset_dir + "/project.bertos", "w")
159 #Create a .spec file in order to make this preset visible in the Wizard
160 open(preset_dir + "/.spec", "w").write("name = '%s preset'" % pname)
162 #Update project makefiles adapting them to the new directory layout
163 mk = open(preset_dir + "/" + pname + ".mk").read()
164 mk = re.sub(r"(%s_SRC_PATH\s*=\s*).*" % pname, r"\1%s" % src_path, mk)
165 mk = re.sub(r"(%s_HW_PATH\s*=\s*).*" % pname, r"\1%s" % hw_path, mk)
167 #remove absolute path from toolchain
168 mk = re.sub(r'(%s_PREFIX\s*=\s*").*?([^/]*")' % pname, r'\1\2', mk)
169 open(preset_dir + "/" + pname + ".mk", "w").write(mk)