Implement build hooks for Windows

This commit is contained in:
Ivan Kravets
2016-08-17 17:36:05 +03:00
parent 9b08244ed8
commit 64cc353455
3 changed files with 86 additions and 52 deletions

View File

@ -68,7 +68,7 @@ commonvars.AddVariables(
DefaultEnvironment(
tools=[
"ar", "as", "gcc", "g++", "gnulink",
"longcmdhook", "platformio", "pioplatform",
"platformio", "pioplatform", "piowinhooks",
"piolib", "piotest", "pioupload", "piomisc"
], # yapf: disable
toolpath=[join(util.get_source_dir(), "builder", "tools")],

View File

@ -1,51 +0,0 @@
# Copyright 2014-present PlatformIO <contact@platformio.org>
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from hashlib import md5
from os.path import join
from tempfile import gettempdir
MAX_SOURCES_LENGTH = 8000 # Windows CLI has limit with command length to 8192
def long_cmd_hook(sources):
_sources = str(sources).replace("\\", "/")
if len(str(_sources)) < MAX_SOURCES_LENGTH:
return sources
tmp_file = join(gettempdir(), "longcmd-%s" % md5(_sources).hexdigest())
with open(tmp_file, "w") as f:
# fix space in paths
for line in _sources.split(".o "):
if not line.endswith(".o"):
line += ".o"
f.write('"%s" ' % line)
return '@"%s"' % tmp_file
def exists(_):
return True
def generate(env):
env.Replace(_long_cmd_hook=long_cmd_hook)
coms = {}
for key in ("ARCOM", "LINKCOM"):
coms[key] = env.get(key, "").replace("$SOURCES",
"${_long_cmd_hook(SOURCES)}")
env.Replace(**coms)
return env

View File

@ -0,0 +1,85 @@
# Copyright 2014-present PlatformIO <contact@platformio.org>
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from hashlib import md5
from os import makedirs
from os.path import isdir, isfile, join
from platform import system
MAX_SOURCES_LENGTH = 8000 # Windows CLI has limit with command length to 8192
def long_sources_hook(env, sources):
_sources = str(sources).replace("\\", "/")
if len(str(_sources)) < MAX_SOURCES_LENGTH:
return sources
# fix space in paths
data = []
for line in _sources.split(".o "):
line = line.strip()
if not line.endswith(".o"):
line += ".o"
data.append('"%s"' % line)
return '@"%s"' % _file_long_data(env, " ".join(data))
def long_incflags_hook(env, incflags):
_incflags = env.subst(incflags).replace("\\", "/")
if len(_incflags) < MAX_SOURCES_LENGTH - 2000:
return incflags
# fix space in paths
data = []
for line in _incflags.split(" -I"):
line = line.strip()
if not line.startswith("-I"):
line = "-I" + line
data.append('-I"%s"' % line[2:])
return '@"%s"' % _file_long_data(env, " ".join(data))
def _file_long_data(env, data):
build_dir = env.subst("$BUILD_DIR")
if not isdir(build_dir):
makedirs(build_dir)
tmp_file = join(build_dir, "longcmd-%s" % md5(data).hexdigest())
if isfile(tmp_file):
return tmp_file
with open(tmp_file, "w") as fp:
fp.write(data)
return tmp_file
def exists(_):
return True
def generate(env):
if system() != "Windows":
return
env.Replace(_long_sources_hook=long_sources_hook)
env.Replace(_long_incflags_hook=long_incflags_hook)
coms = {}
for key in ("ARCOM", "LINKCOM"):
coms[key] = env.get(key, "").replace(
"$SOURCES", "${_long_sources_hook(__env__, SOURCES)}")
coms['_CCCOMCOM'] = env.get("_CCCOMCOM", "").replace(
"$_CPPINCFLAGS", "${_long_incflags_hook(__env__, _CPPINCFLAGS)}")
env.Replace(**coms)
return env