private void LoadTargetTasks(ProjectTargetElement target, List<EnvVar> vars, BuildToolDefine tool, ToolOptions toolOptions, params List<ToolFlag>[] commonFlags)
        {
            foreach (ProjectTaskElement task in target.Tasks)
            {
                BuildScript script = new BuildScript();

                string exeCmdEnv = ToEnvVar(tool.Description.ToLower()).Trim();
                string exeCmd = tool.Description.ToLower();

                int idx = -1;
                script.Conditional = task.Condition;
                script.Script = task.GetParameter(TaskScriptAttributeName);
                if (!string.IsNullOrEmpty(script.Script))
                {
                    if ((idx = script.Script.ToLower().IndexOf(exeCmdEnv)) >= 0)
                    {
                        // remove exe command and 
                        script.Script = script.Script.Remove(idx, exeCmdEnv.Length);

                        script.Script = RemoveFlagsAndWrapper(script.Script, toolOptions, commonFlags);

                        toolOptions.BuildToolParameters.Parameters.Add(script);
                    }
                    else if ((idx = script.Script.ToLower().IndexOf(exeCmd)) >= 0)
                    {
                        // remove exe command and 
                        script.Script = script.Script.Remove(idx, exeCmd.Length);

                        script.Script = RemoveFlagsAndWrapper(script.Script, toolOptions, commonFlags);

                        toolOptions.BuildToolParameters.Parameters.Add(script);
                    }
                    else if (toolOptions.BuildToolParameters.Parameters.Count > 0)
                    {
                        toolOptions.BuildToolParameters.PostBuild.Add(script);
                    }
                    else
                    {
                        toolOptions.BuildToolParameters.PreBuild.Add(script);
                    }
                }
                else if(string.Compare(task.Name, SetEnvScriptTag) == 0)
                {
                    EnvVar var = new EnvVar();
                    var.Name = task.GetParameter("Name");
                    var.Conditional = task.Condition;
                    var.Value = task.GetParameter("Value");
                    vars.Add(var);
                }
            }
        }
 public void CopyTo(EnvVar dest)
 {
     CopyHelper.CopyTo(this, dest);
 }
        private MemberPrecidence LoadScatterElements(object owner, object conditional, List<MemoryMap> maps, ArrayList conditionalStack)
        {
            MemberPrecidence ret = MemberPrecidence.Unknown;

            List<If> ifs = GetCollectionFromObject(conditional, typeof(List<If>)) as List<If>;
            if (ifs != null && ifs.Count > 0)
            {
                LoadScatterConditional(owner, ifs, maps, conditionalStack);
            }
            List<IfDefined> ifdefs = GetCollectionFromObject(conditional, typeof(List<IfDefined>)) as List<IfDefined>;
            if (ifdefs != null)
            {
                LoadScatterConditional(owner, ifdefs, maps, conditionalStack);
            }
            List<IfNotDefined> ifndefs = GetCollectionFromObject(conditional, typeof(List<IfNotDefined>)) as List<IfNotDefined>;
            if (ifndefs != null)
            {
                LoadScatterConditional(owner, ifndefs, maps, conditionalStack);
            }

            List<LoadRegion> lrCollection = GetCollectionFromObject(conditional, typeof(List<LoadRegion>)) as List<LoadRegion>;
            if (lrCollection != null && lrCollection.Count > 0)
            {
                List<MemoryRegion> regionSet = GetCollectionFromObject(owner, typeof(List<MemoryRegion>)) as List<MemoryRegion>;

                ret = MemberPrecidence.LoadRegion;

                foreach (LoadRegion lr in lrCollection)
                {
                    MemoryRegion region = new MemoryRegion();
                    region.Name = lr.Name;
                    region.Address = lr.Base;
                    region.Options = lr.Options;
                    region.Size = lr.Size;
//                    region.Order = regionSet.Count;
                    region.Conditional = GetConditionalValue(conditional);

                    regionSet.Add(region);

                    ret = LoadScatterElements(region, lr, maps, conditionalStack);
                }
            }

            List<ExecRegion> erCollection = GetCollectionFromObject(conditional, typeof(List<ExecRegion>)) as List<ExecRegion>;
            if (erCollection != null && erCollection.Count > 0)
            {
                List<MemorySection> sectionSet = GetCollectionFromObject(owner, typeof(List<MemorySection>)) as List<MemorySection>;

                ret = MemberPrecidence.ExecRegion;

                foreach (ExecRegion er in erCollection)
                {
                    MemorySection section = new MemorySection();
                    section.Name        = er.Name;
                    section.Address     = er.Base;
                    section.Options     = er.Options;
                    section.Size        = er.Size;
                    section.Order       = (int)m_execRegionOrderMap[(owner as MemoryRegion).Name + ":" + er.Name];
                    section.Conditional = GetConditionalValue(conditional);

                    sectionSet.Add(section);

                    ret = LoadScatterElements(section, er, maps, conditionalStack);
                }
            }

            List<FileMapping> fileCollection = GetCollectionFromObject(conditional, typeof(List<FileMapping>)) as List<FileMapping>;
            if (fileCollection != null && fileCollection.Count > 0)
            {
                List<MemorySymbol> symSet = GetCollectionFromObject(owner, typeof(List<MemorySymbol>)) as List<MemorySymbol>;

                ret = MemberPrecidence.FileMapping;


                foreach (FileMapping fm in fileCollection)
                {
                    MemorySymbol sym = new MemorySymbol();
                    sym.Name       = fm.Name;
                    sym.Options    = fm.Options;
                    sym.Conditional = GetConditionalValue(conditional);

                    symSet.Add(sym);
                }
            }

            List<Set> setCollection = GetCollectionFromObject(conditional, typeof(List<Set>)) as List<Set>;
            if (setCollection != null && setCollection.Count > 0)
            {
                EnvVars envSets = GetObjectFromProperty(owner, typeof(EnvVars)) as EnvVars;

                if (ret < MemberPrecidence.SetMember)
                {
                    ret = MemberPrecidence.SetMember;
                }

                EnvVars envSet = null;

                // conditional belongs to the map if we have a load region in this conditional
                if(conditionalStack.Count == 0)
                {
                    envSet = FindMatchingSet(envSets, "Global");

                    if (envSet == null)
                    {
                        envSet = new EnvVars();
                        envSet.Name = "Global";
                        envSets.EnvVarsCollection.Add(envSet);
                    }
                }
                else
                {
                    for (int i = 0; i < conditionalStack.Count; i++)
                    {
                        object cond = conditionalStack[i];

                        if (i == (conditionalStack.Count-1) && owner is MemoryMap && ret >= MemberPrecidence.LoadRegion)
                        {
                            ((MemoryMap)owner).Name        = GetConditionalName(conditional);
                            ((MemoryMap)owner).Conditional = GetConditionalValue(conditional);
                        }
                        else
                        {
                            string name = GetConditionalName(cond);
                            string value = GetConditionalValue(cond);

                            name += "_" + value.Replace(" ", "_").Replace("\"", "");

                            envSet = FindMatchingSet(envSets, name);

                            if (envSet == null)
                            {
                                envSet = new EnvVars();
                                envSet.Name = name;
                                envSet.Conditional = value;

                                envSets.EnvVarsCollection.Add(envSet);
                            }

                            //envSets = envSet.EnvVarsCollection;
                        }
                    }
                }


                if ((int)ret < (int)MemberPrecidence.SetMember)
                {
                    ret = MemberPrecidence.SetMember;
                }

                foreach (Set set in GetCollectionFromObject(conditional, typeof(List<Set>)))
                {
                    EnvVar var = new EnvVar();
                    var.Name = set.Name;
                    var.Value = set.Value;

                    envSet.EnvVarCollection.Add(var);
                }
            }

            return ret;
        }
        void SaveGridView()
        {
            dataGridView.EndEdit();
            if (dataGridView.Tag is EnvVars)
            {
                List<EnvVar> evc = (dataGridView.Tag as EnvVars).EnvVarCollection;

                evc.Clear();
                foreach (DataGridViewRow row in dataGridView.Rows)
                {
                    if (row.Cells[0].Value != null)
                    {
                        EnvVar env = new EnvVar();
                        env.Name = row.Cells[0].Value.ToString();
                        env.Value = (row.Cells[1].Value == null ? "" : row.Cells[1].Value.ToString());
                        env.Conditional = row.Cells[2].Value == null? "" : row.Cells[2].Value.ToString();
                        evc.Add(env);
                    }
                }
            }
            else if (dataGridView.Tag is List<ToolFlag>)
            {
                List<ToolFlag> flags = (dataGridView.Tag as List<ToolFlag>);

                flags.Clear();
                foreach (DataGridViewRow row in dataGridView.Rows)
                {
                    if (row.Cells[0].Value != null || row.Cells[1].Value != null)
                    {
                        ToolFlag flag = new ToolFlag();
                        flag.Conditional = (row.Cells[0].Value == null ? "" : row.Cells[0].Value.ToString());
                        flag.Flag = (row.Cells[1].Value == null ? "" : row.Cells[1].Value.ToString());
                        flags.Add(flag);
                    }
                }
            }
            else if (dataGridView.Tag is List<BuildParameter>)
            {
                List<BuildParameter> parms = (dataGridView.Tag as List<BuildParameter>);

                parms.Clear();
                foreach (DataGridViewRow row in dataGridView.Rows)
                {
                    if (row.Cells[0].Value != null && row.Cells[1].Value != null)
                    {
                        BuildParameter parm = new BuildParameter();
                        parm.Parameter   = (string)row.Cells[0].Value;
                        parm.Action      = (string)row.Cells[1].Value;
                        parm.Description = (string)row.Cells[2].Value;
                        parms.Add(parm);
                    }
                }
            }
            else if (dataGridView.Tag is List<MemorySymbol>)
            {
                List<MemorySymbol> msc = dataGridView.Tag as List<MemorySymbol>;

                msc.Clear();
                foreach (DataGridViewRow row in dataGridView.Rows)
                {
                    if (row.Cells[0].Value != null && row.Cells[1].Value != null)
                    {
                        MemorySymbol parm = new MemorySymbol();
                        parm.Name        = (string)row.Cells[0].Value;
                        parm.Options     = (string)row.Cells[1].Value;
                        parm.Description = (string)row.Cells[2].Value;
                        parm.Conditional = (string)row.Cells[3].Value;
                        msc.Add(parm);
                    }
                }
            }
            else if (dataGridView.Tag is List<BuildScript>)
            {
                List<BuildScript> al = dataGridView.Tag as List<BuildScript>;
                al.Clear();

                foreach (DataGridViewRow row in dataGridView.Rows)
                {
                    if (row.Cells[1].Value != null)
                    {
                        BuildScript bs = new BuildScript();
                        bs.Conditional = row.Cells[0].Value as string;
                        bs.Script = row.Cells[1].Value as string;

                        al.Add(bs);
                    }
                }
            }
            else if (dataGridView.Tag is List<ApiTemplate>)
            {
                List<ApiTemplate> al = dataGridView.Tag as List<ApiTemplate>;
                al.Clear();

                foreach (DataGridViewRow row in dataGridView.Rows)
                {
                    if (!string.IsNullOrEmpty(row.Cells[0].Value as string))
                    {
                        ApiTemplate at = new ApiTemplate();
                        at.FilePath = row.Cells[0].Value as string;
                        at.Description = row.Cells[1].Value as string;

                        al.Add(at);
                    }
                }
            }
            else if (dataGridView.Tag is MFComponent)
            {
                foreach (DataGridViewRow row in dataGridView.Rows)
                {
                    /*
                    if ((bool)row.Cells[1].Value)
                    {
                        Library lib = row.Tag as Library;
                        MFComponent comp = dataGridView.Tag as MFComponent;
                        comp.Guid = lib.Guid;
                        comp.Name = lib.Name;
                        break;
                    }
                    */
                }
            }
        }