#!python3
# Copyright 2007-2017 Gemr. All Rights Reserved.
# Licensed to MIT see LICENSE.txt
from enum import Enum
__author__ = 'Suryavarman (http://sourceforge.net/u/suryavarman/profile/)'
[docs]class AdressModel(Enum):
""" Define a adress model
`Wikipedia <https://en.wikipedia.org/wiki/X86-64/>`_
"""
x86 = 1
x64 = 2
[docs]class Target(Enum):
""" Define the linking : statically or dynamically
Associate with the debugging symbol or not.
"""
release_static = 1
release_dynamic = 2
debug_static = 3
debug_dynamic = 4
[docs]class IdeArgs:
def __init__(self, inArgs={}, inCompilersArgs={}):
"""
:param inArgs: The ide arguments
:type inArgs: dict { str : str }
:param inCompilersArgs:
**Example**
>>> { "Compiler A Type Name" : { "KEYA_1" : "VALUEA_1", "KEYA_2" : ["VALUEA_2", "VALUEA_3", "VALUEA_4"] }, "Compiler B Type Name" : { "KEYB_1" : "VALUEB_1", "KEYB_2" : ["VALUEB_2", "VALUEB_3", "VALUEB_4"] }}
:type inCompilersArgs: dict { str : dict { str : str or [str] } }
.. note::
The dictionary can be also a `collections.OrderedDict <https://docs.python.org/3.1/library/collections.html#collections.OrderedDict>`_
to preserve the order.
>>> collections.OrderedDict( sorted( { '--ARG1' : 'value1', '--ARG2' : 'value2' }.items(), key=lambda t: t[0]))
"""
self.m_Args = inArgs
self.m_CompilersArgs = inCompilersArgs
[docs] def getCompilerArgs(self, inCompiler):
"""
:param inCompiler: A compiler instance to retrieve with the type the associate argument
:type inCompiler: gdeps.Compiler
:return: The associate value, usually it's a string.
:rtype: dict { str : str }
"""
return self.m_CompilersArgs[inCompiler.m_TypeName] if inCompiler.m_TypeName in self.m_CompilersArgs else {}
[docs]class TargetArgs:
""" Define the target arguments for each case """
def __init__(self, inTargetType, inArgs, inx86Args={}, inx64Args={}, inCompilersArgs={}, inIdeArgs={}):
"""
:param inTargetType:
:type inTargetType: Target
:param inArgs:
:type inArgs: dict { str : str }
:param inx86Args: Arguments for the X86 platform
:type inx86Args: dict { str : str } or dict { str : [str] }
:param inx64Args: Arguments for the X64 platform
:type inx64Args: dict { str : str } or dict { str : [str] }
:param inIdeArgs: The ides associated arguments
**Example**
>>> {gdeps.Codeblocks.getTypeName(): IdeArgs( inArgs={"KEY": "VALUE"}, inCompilersArgs={ "Compiler A Type Name" : { "KEYA_1" : "VALUEA_1", "KEYA_2" : ["VALUEA_2", "VALUEA_3", "VALUEA_4"] }, "Compiler B Type Name" : { "KEYB_1" : "VALUEB_1", "KEYB_2" : ["VALUEB_2", "VALUEB_3", "VALUEB_4"] }})
:type inIdeArgs: dict { str : IdeArgs }
:param inCompilersArgs:
**Example**
>>> { "Compiler A Type Name" : { "KEYA_1" : "VALUEA_1", "KEYA_2" : ["VALUEA_2", "VALUEA_3", "VALUEA_4"] }, "Compiler B Type Name" : { "KEYB_1" : "VALUEB_1", "KEYB_2" : ["VALUEB_2", "VALUEB_3", "VALUEB_4"] }}
:type inCompilersArgs: dict { str : dict { str : str or array of str } }
.. note::
The dictionary can be also a `collections.OrderedDict <https://docs.python.org/3.1/library/collections.html#collections.OrderedDict>`_
to preserve the order.
>>> collections.OrderedDict( sorted( { '--ARG1' : 'value1', '--ARG2' : 'value2' }.items(), key=lambda t: t[0]))
"""
self.m_TargetType = inTargetType
self.m_Args = inArgs
self.m_x86Args = inx86Args
self.m_x64Args = inx64Args
self.m_CompilersArgs = inCompilersArgs
self.m_IdesArgs = inIdeArgs
# [AdressModel][Ide Name] : TargetName
[docs] def getAdressModelArgs(self, inAdressModel):
"""
:param inAdressModel: The selected address model
:type inAdressModel: AdressModel
:return: The associate arguments
:rtype: dict { str : str } or dict { str : [str] }
"""
if inAdressModel == AdressModel.x86:
return self.m_x86Args
if inAdressModel == AdressModel.x64:
return self.m_x64Args
else:
return {}
@staticmethod
[docs] def joinArgumentsDictionary(inDic, inDicOut):
print(inDic)
for aKey, aValue in inDic.items():
# https://wiki.python.org/moin/KeyError
inDicOut.setdefault(aKey, [])
if type(aValue) is list or type(aValue) is tuple:
for item in aValue:
inDicOut[aKey].append(item)
else:
inDicOut[aKey].append(aValue)
[docs] def getCompilerArgs(self, inCompiler, inIde=None):
"""
:param inCompiler: A compiler instance to retrieve with the type the associate argument
:type inCompiler: gdeps.Compiler
:param inIde: A ide instance to retrieve with the type the compiler arguments associate with the IDE
:type inIde: gdeps.Ide
:return: The associate value, usually it's a string.
:rtype: dict { str : str }
"""
ide_args = self.m_IdesArgs[inIde.m_TypeName] if inIde is not None and inIde.m_TypeName in self.m_IdesArgs else IdeArgs({}, {})
result = self.m_CompilersArgs[inCompiler.m_TypeName] if inCompiler.m_TypeName in self.m_CompilersArgs else {}
TargetArgs.joinArgumentsDictionary(ide_args.m_Args, result)
TargetArgs.joinArgumentsDictionary(ide_args.getCompilerArgs(inCompiler), result)
return result
[docs] def joinArgs(self, inCompiler):
""" Join all the target arguments to one dictionary define for this compiler.
:param inCompiler: A compiler instance to retrieve with the type the associate argument
:type inCompiler: gdeps.Compiler
:return: The associate values
:rtype: dict { str : array of str }
"""
aResult = self.m_Args
TargetArgs.joinArgumentsDictionary(self.getAdressModelArgs(inCompiler.getAdressModel()), aResult)
TargetArgs.joinArgumentsDictionary(self.getCompilerArgs(inCompiler), aResult)
return aResult