Refactor to use new protocol module and sipo.
[bertos.git] / wizard / compatibility.py
1 #!/usr/bin/env python
2 # encoding: utf-8
3 #
4 # This file is part of BeRTOS.
5 #
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.
10 #
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.
15 #
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
19 #
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.
28 #
29 # Copyright 2010 Develer S.r.l. (http://www.develer.com/)
30 #
31 #
32 # Author: Lorenzo Berni <duplo@develer.com>
33 #
34
35 import const
36 import os
37 from bertos_utils import replaceSeparators, csrcGenerator
38
39 def _userMkGenerator(project_info):
40     makefile = open(os.path.join(const.DATA_DIR, "mktemplates/old/template.mk"), "r").read()
41     destination = os.path.join(project_info.prjdir, os.path.basename(project_info.prjdir) + ".mk")
42     # Deadly performances loss was here :(
43     mk_data = {}
44     mk_data["$pname"] = os.path.basename(project_info.info("PROJECT_PATH"))
45     mk_data["$main"] = os.path.basename(project_info.info("PROJECT_PATH")) + "/main.c"
46     for key in mk_data:
47         makefile = makefile.replace(key, mk_data[key])
48     open(destination, "w").write(makefile)
49
50 def _mkGenerator(project_info):
51     """
52     Generates the mk file for the current project.
53     """
54     makefile = open(os.path.join(const.DATA_DIR, "mktemplates/old/template_wiz.mk"), "r").read()
55     destination = os.path.join(project_info.prjdir, os.path.basename(project_info.prjdir) + "_wiz.mk")
56     mk_data = {}
57     mk_data["$pname"] = project_info.info("PROJECT_NAME")
58     mk_data["$cpuclockfreq"] = project_info.info("SELECTED_FREQ")
59     cpu_mk_parameters = []
60     for key, value in project_info.info("CPU_INFOS").items():
61         if key.startswith(const.MK_PARAM_ID):
62             cpu_mk_parameters.append("%s = %s" %(key.replace("MK", mk_data["$pname"]), value))
63     mk_data["$cpuparameters"] = "\n".join(cpu_mk_parameters)
64     mk_data["$csrc"], mk_data["$pcsrc"], mk_data["$cppasrc"], mk_data["$cxxsrc"], mk_data["$asrc"], mk_data["$constants"] = csrcGenerator(project_info)
65     mk_data["$prefix"] = replaceSeparators(project_info.info("TOOLCHAIN")["path"].split("gcc")[0])
66     mk_data["$suffix"] = replaceSeparators(project_info.info("TOOLCHAIN")["path"].split("gcc")[1])
67     mk_data["$main"] = os.path.basename(project_info.info("PROJECT_PATH")) + "/main.c"
68     for key in mk_data:
69         makefile = makefile.replace(key, mk_data[key])
70     open(destination, "w").write(makefile)
71
72 def _makefileGenerator(project_info):
73     """
74     Generate the Makefile for the current project.
75     """
76     makefile = open(os.path.join(const.DATA_DIR, "mktemplates/old/Makefile"), "r").read()
77     destination = os.path.join(project_info.maindir, "Makefile")
78     # TODO write a general function that works for both the mk file and the Makefile
79     makefile = makefile.replace("$pname", project_info.info("PROJECT_NAME"))
80     open(destination, "w").write(makefile)
81
82 def updateProject(project_data):
83     """
84     Update incrementally the project_data loaded from a BeRTOS Wizard project
85     file.
86     """
87     wizard_version = project_data.get("WIZARD_VERSION", 0)
88     if wizard_version < 1:
89         # Ignore the BERTOS_PATH inside the project file for older project
90         project_data["SOURCES_PATH"] = project_dir
91     if wizard_version < 2:
92         # Use SOURCES_PATH instead of BERTOS_PATH for backward compatibility
93         project_data["BERTOS_PATH"] = project_data["SOURCES_PATH"]
94     if wizard_version < 3:
95         # Use older makefile templates and generators using monkey patching
96         import bertos_utils
97         bertos_utils.mkGenerator = _mkGenerator
98         bertos_utils.userMkGenerator = _userMkGenerator
99         bertos_utils.makefileGenerator = _makefileGenerator
100     if wizard_version < 4:
101         # Use default PROJECT_SRC_PATH_FROM_MAKEFILE and
102         # PROJECT_HW_PATH_FROM_MAKEFILE
103         project_data["PROJECT_SRC_PATH_FROM_MAKEFILE"] = project_data["PROJECT_SRC_PATH"]
104         project_data["PROJECT_HW_PATH_FROM_MAKEFILE"] = project_data["PROJECT_HW_PATH"]
105     return project_data
106