Welcome to mirror list, hosted at ThFree Co, Russian Federation.

CompilerArguments.fs « Services « MonoDevelop.FSharpBinding « fsharpbinding « external « main - github.com/mono/monodevelop.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 622d3278445a9a4e0ca458e5f744186d92129595 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
// --------------------------------------------------------------------------------------
// Common utilities for environment, debugging and working with project files
// --------------------------------------------------------------------------------------

namespace MonoDevelop.FSharp

open System
open System.IO
open System.Reflection
open System.Globalization
open System.Runtime.Versioning
open MonoDevelop.Projects
open MonoDevelop.Ide
open MonoDevelop.Core.Assemblies
open MonoDevelop.Core
open ExtCore
open ExtCore.Control
open Microsoft.FSharp.Compiler.SourceCodeServices

// --------------------------------------------------------------------------------------
// Common utilities for working with files & extracting information from
// MonoDevelop objects (e.g. references, project items etc.)
// --------------------------------------------------------------------------------------

module CompilerArguments =

  /// Wraps the given string between double quotes
  let wrapFile (s:string) = if s.StartsWith "\"" then s else "\"" + s + "\""

  // Translate the target framework to an enum used by FSharp.CompilerBinding
  let getTargetFramework (targetFramework:TargetFrameworkMoniker) =
      if targetFramework = TargetFrameworkMoniker.NET_3_5 then FSharpTargetFramework.NET_3_5
      elif targetFramework = TargetFrameworkMoniker.NET_3_0 then FSharpTargetFramework.NET_3_0
      elif targetFramework = TargetFrameworkMoniker.NET_2_0 then FSharpTargetFramework.NET_2_0
      elif targetFramework = TargetFrameworkMoniker.NET_4_0 then FSharpTargetFramework.NET_4_0
      elif targetFramework = TargetFrameworkMoniker.NET_4_5 then FSharpTargetFramework.NET_4_5
      elif targetFramework = TargetFrameworkMoniker.NET_4_6 then FSharpTargetFramework.NET_4_6
      elif targetFramework = TargetFrameworkMoniker.NET_4_6_1 then FSharpTargetFramework.NET_4_6_1
      elif targetFramework = TargetFrameworkMoniker.NET_4_6_2 then FSharpTargetFramework.NET_4_6_2
      else FSharpTargetFramework.NET_4_6_2

  module Project =
      ///Use the IdeApp.Workspace active configuration failing back to proj.DefaultConfiguration then ConfigurationSelector.Default
      let getCurrentConfigurationOrDefault (proj:Project) =
          match IdeApp.Workspace with
          | ws when ws <> null && ws.ActiveConfiguration <> null -> ws.ActiveConfiguration
          | _ -> if proj <> null then proj.DefaultConfiguration.Selector
                 else ConfigurationSelector.Default

      let isPortable (project: DotNetProject) =
          not (String.IsNullOrEmpty project.TargetFramework.Id.Profile)
      
      let isDotNetCoreProject (project:DotNetProject) =
          let properties = project.MSBuildProject.EvaluatedProperties
          properties.HasProperty ("TargetFramework") || properties.HasProperty ("TargetFrameworks")

      let getDefaultTargetFramework (runtime:TargetRuntime) =
          let newest_net_framework_folder (best:TargetFramework,best_version:int[]) (candidate_framework:TargetFramework) =
              if runtime.IsInstalled(candidate_framework) && candidate_framework.Id.Identifier = TargetFrameworkMoniker.ID_NET_FRAMEWORK then
                  let version = candidate_framework.Id.Version
                  let parsed_version_s = (if version.[0] = 'v' then version.[1..] else version).Split('.')
                  let parsed_version =
                      try
                          Array.map int parsed_version_s
                      with
                          | _ -> [| 0 |]
                  let mutable level = 0
                  let mutable cont = true
                  let min_level = min parsed_version.Length best_version.Length
                  let mutable new_best = false
                  while cont && level < min_level do
                      if parsed_version.[level] > best_version.[level] then
                          new_best <- true
                          cont <- false
                      elif best_version.[level] > parsed_version.[level] then
                          cont <- false
                      else
                          cont <- true
                      level <- level + 1
                  if new_best then
                      (candidate_framework, parsed_version)
                  else
                      (best,best_version)
              else
                  (best,best_version)
          let candidate_frameworks = MonoDevelop.Core.Runtime.SystemAssemblyService.GetTargetFrameworks()
          let first = Seq.head candidate_frameworks
          let best_info = Seq.fold newest_net_framework_folder (first,[| 0 |]) candidate_frameworks
          fst best_info

  module ReferenceResolution =
    
    let tryGetDefaultReference langVersion targetFramework filename (extrapath: string option) =
        let dirs =
            match extrapath with
            | Some path -> path :: FSharpEnvironment.getDefaultDirectories(langVersion, targetFramework)
            | None -> FSharpEnvironment.getDefaultDirectories(langVersion, targetFramework)
        FSharpEnvironment.resolveAssembly dirs filename

  let resolutionFailedMessage (n:string) = String.Format ("Resolution: Assembly resolution failed when trying to find default reference for: {0}", n)
  /// Generates references for the current project & configuration as a
  /// list of strings of the form [ "-r:<full-path>"; ... ]
  let generateReferences (project: DotNetProject, projectAssemblyReferences: AssemblyReference seq, langVersion, targetFramework, shouldWrap) =
       [
        let wrapf = if shouldWrap then wrapFile else id

        let getAbsolutePath (ref:AssemblyReference) =
            let assemblyPath = ref.FilePath
            if assemblyPath.IsAbsolute then
                assemblyPath.FullPath |> string
            else
                let s = Path.Combine(project.FileName.ParentDirectory.ToString(), assemblyPath.ToString())
                Path.GetFullPath s

        let projectReferences =
            projectAssemblyReferences
            |> Seq.map getAbsolutePath
            |> Seq.distinct

        let find assemblyName=
            projectReferences
            |> Seq.tryFind (fun fn -> fn.EndsWith(assemblyName + ".dll", true, CultureInfo.InvariantCulture)
                                      || fn.EndsWith(assemblyName, true, CultureInfo.InvariantCulture))


        // If 'mscorlib.dll' or 'FSharp.Core.dll' is not in the set of references, we try to resolve and add them.
        match find "FSharp.Core", find "mscorlib", Project.isDotNetCoreProject project with
        | None, Some mscorlib, false ->
            // if mscorlib is found without FSharp.Core yield fsharp.core in the same base dir as mscorlib
            // falling back to one of the default directories
            let extraPath = Some (Path.GetDirectoryName (mscorlib))
            match ReferenceResolution.tryGetDefaultReference langVersion targetFramework "FSharp.Core" extraPath with
            | Some ref -> yield "-r:" + wrapf(ref)
            | None -> LoggingService.LogWarning(resolutionFailedMessage "FSharp.Core")
        | None, None, false ->
            // If neither are found yield the default fsharp.core
            match ReferenceResolution.tryGetDefaultReference langVersion targetFramework "FSharp.Core" None with
            | Some ref -> yield "-r:" + wrapf(ref)
            | None -> LoggingService.LogWarning(resolutionFailedMessage "FSharp.Core")
        | _ -> () // found them both, no action needed

        for file in projectReferences do
            yield "-r:" + wrapf(file) ]

  let generateDebug (config:FSharpCompilerParameters) =
      match config.ParentConfiguration.DebugSymbols, config.ParentConfiguration.DebugType with
      | true, typ ->
          match typ with
          | "full" -> "--debug:full"
          | "pdbonly" -> "--debug:pdbonly"
          | _ -> "--debug+"
      | false, _ -> "--debug-"

  let getSharedAssetFilesFromReferences (project:DotNetProject) =
      project.References
      |> Seq.filter (fun r -> r.ExtendedProperties.Contains("MSBuild.SharedAssetsProject"))
      |> Seq.collect (fun r -> (r.ResolveProject project.ParentSolution).Files)
      |> Seq.map (fun f -> f.FilePath)
      |> Set.ofSeq

  let getCompiledFiles project =
      let sharedAssetFiles = getSharedAssetFilesFromReferences project

      project.Files
      // Shared Asset files need to be referenced first
      |> Seq.sortByDescending (fun f -> sharedAssetFiles.Contains f.FilePath)
      |> Seq.filter(fun f -> f.FilePath.Extension = ".fs")
      |> Seq.map(fun f -> f.Name)
      |> Seq.distinct

  /// Generates command line options for the compiler specified by the
  /// F# compiler options (debugging, tail-calls etc.), custom command line
  /// parameters and assemblies referenced by the project ("-r" options)
  let generateCompilerOptions (project:DotNetProject, projectAssemblyReferences: AssemblyReference seq, fsconfig:FSharpCompilerParameters, reqLangVersion, targetFramework, configSelector, shouldWrap) =
    let dashr = generateReferences (project, projectAssemblyReferences, reqLangVersion, targetFramework, shouldWrap) |> Array.ofSeq

    let splitByChars (chars: char array) (s:string) =
        s.Split(chars, StringSplitOptions.RemoveEmptyEntries)

    let defines = fsconfig.GetDefineSymbols()
    [
       yield "--simpleresolution"
       yield "--noframework"
       let outputFile = project.GetOutputFileName(configSelector).ToString()
       if not (String.IsNullOrWhiteSpace outputFile) then 
           yield "--out:" + outputFile
       if Project.isPortable project || Project.isDotNetCoreProject project then
           yield "--targetprofile:netcore"
       if not (String.IsNullOrWhiteSpace fsconfig.PlatformTarget) then
           yield "--platform:" + fsconfig.PlatformTarget
       yield "--fullpaths"
       yield "--flaterrors"
       for symbol in defines do yield "--define:" + symbol
       yield if fsconfig.HasDefineSymbol "DEBUG" then  "--debug+" else  "--debug-"
       yield if fsconfig.Optimize then "--optimize+" else "--optimize-"
       yield if fsconfig.GenerateTailCalls then "--tailcalls+" else "--tailcalls-"
       if not (String.IsNullOrWhiteSpace fsconfig.DebugType) then
           yield sprintf "--debug:%s" fsconfig.DebugType
       yield match project.CompileTarget with
             | CompileTarget.Library -> "--target:library"
             | CompileTarget.Module -> "--target:module"
             | _ -> "--target:exe"
       yield if fsconfig.TreatWarningsAsErrors then "--warnaserror+" else "--warnaserror-"
       yield sprintf "--warn:%d" fsconfig.WarningLevel
       if not (String.IsNullOrWhiteSpace fsconfig.NoWarn) then
           for arg in fsconfig.NoWarn |> splitByChars [|';'; ','|] do
               yield "--nowarn:" + arg
       // TODO: This currently ignores escaping using "..."
       for arg in fsconfig.OtherFlags |> splitByChars [|' '|] do
         yield arg
       yield! dashr
       yield! (getCompiledFiles project)]

  let generateProjectOptions (project:DotNetProject, projectAssemblyReferences: AssemblyReference seq, fsconfig:FSharpCompilerParameters, reqLangVersion, targetFramework, configSelector, shouldWrap) =
    let compilerOptions = generateCompilerOptions (project, projectAssemblyReferences, fsconfig, reqLangVersion, targetFramework, configSelector, shouldWrap) |> Array.ofSeq
    let loadedTimeStamp =  DateTime.MaxValue // Not 'now', we don't want to force reloading
    { ProjectFileName = project.FileName.FullPath.ToString()
      SourceFiles = [| |]
      Stamp = None
      OtherOptions = compilerOptions
      ReferencedProjects = [| |]
      IsIncompleteTypeCheckEnvironment = false
      UseScriptResolutionRules = false
      LoadTime = loadedTimeStamp
      UnresolvedReferences = None
      OriginalLoadReferences = []
      ExtraProjectInfo = None
      ProjectId = None }

  /// Get source files of the current project (returns files that have
  /// build action set to 'Compile', but not e.g. scripts or resources)
  let getSourceFiles (items:ProjectItemCollection) =
      [ for file in items.GetAll<ProjectFile>() do
            if file.BuildAction = "Compile" && file.Subtype <> Subtype.Directory then
                yield file.FilePath.FullPath.ToString() ]


  /// Generate inputs for the compiler (excluding source code!); returns list of items
  /// containing resources (prefixed with the --resource parameter)
  let generateOtherItems (items:ProjectItemCollection) =
    [ for file in items.GetAll<ProjectFile>() do
          match file.BuildAction with
          | _ when file.Subtype = Subtype.Directory -> ()
          | "EmbeddedResource" ->
              let fileName = file.Name.ToString()
              let logicalResourceName = file.ProjectVirtualPath.ToString().Replace("\\",".").Replace("/",".")
              yield "--resource:" + wrapFile fileName + "," + wrapFile logicalResourceName
          | "None" | "Content" | "Compile" -> ()
          | _ -> ()] // failwith("Items of type '" + s + "' not supported") ]

  let private getToolPath (pathsToSearch:seq<string>) (extensions:seq<string>) (toolName:string) =
      let filesToSearch = Seq.map (fun x -> toolName + x) extensions

      let tryFindPathAndFile (filesToSearch:seq<string>) (path:string) =
          try
              let candidateFiles = Directory.GetFiles(path)

              let fileIfExists candidateFile =
                  Seq.tryFind (fun x -> Path.Combine(path,x) = candidateFile) filesToSearch
              match Seq.tryPick fileIfExists candidateFiles with
              | Some x -> Some(path,x)
              | None -> None

          with
          | e -> None

      Seq.tryPick (tryFindPathAndFile filesToSearch) pathsToSearch

  /// Get full path to tool
  let getEnvironmentToolPath (runtime:TargetRuntime) (framework:TargetFramework) (extensions:seq<string>) (toolName:string) =

      let pathsToSearch = runtime.GetToolsPaths(framework)
      getToolPath pathsToSearch extensions toolName

  let private getShellToolPath (extensions:seq<string>) (toolName:string)  =
    let pathVariable = Environment.GetEnvironmentVariable("PATH")
    let searchPaths = pathVariable.Split [| IO.Path.PathSeparator  |]
    getToolPath searchPaths extensions toolName

  let getDefaultInteractive() =

      let runtime = IdeApp.Preferences.DefaultTargetRuntime.Value
      let framework = Project.getDefaultTargetFramework runtime

      match getEnvironmentToolPath runtime framework [|""; ".exe"; ".bat" |] "fsharpi" with
      | Some(dir,file)-> Some(Path.Combine(dir,file))
      | None->
      match getShellToolPath [| ""; ".exe"; ".bat" |] "fsharpi" with
      | Some(dir,file)-> Some(Path.Combine(dir,file))
      | None->
      match getEnvironmentToolPath runtime framework [|""; ".exe"; ".bat" |] "fsi" with
      | Some(dir,file)-> Some(Path.Combine(dir,file))
      | None->
      match getShellToolPath [| ""; ".exe"; ".bat" |] "fsi" with
      | Some(dir,file)-> Some(Path.Combine(dir,file))
      | None->
      match FSharpEnvironment.BinFolderOfDefaultFSharpCompiler None with
      | Some(dir) when FSharpEnvironment.safeExists(Path.Combine(dir, "fsi.exe")) ->
          Some(Path.Combine(dir,"fsi.exe"))
      | _ -> None

  let getCompilerFromEnvironment (runtime:TargetRuntime) (framework:TargetFramework) =
      match getEnvironmentToolPath runtime framework [| ""; ".exe"; ".bat" |] "fsharpc" with
      | Some(dir,file) -> Some(Path.Combine(dir,file))
      | None ->
      match getEnvironmentToolPath runtime framework [| ""; ".exe"; ".bat" |] "fsc" with
      | Some(dir,file) -> Some(Path.Combine(dir,file))
      | None -> None

  // Only used when xbuild support is not enabled. When xbuild is enabled, the .targets
  // file finds FSharp.Build.dll which finds the F# compiler.
  let getDefaultFSharpCompiler() =

      let runtime = IdeApp.Preferences.DefaultTargetRuntime.Value
      let framework = Project.getDefaultTargetFramework runtime

      match getCompilerFromEnvironment runtime framework with
      | Some(result)-> Some(result)
      | None->
      match getShellToolPath [| ""; ".exe"; ".bat" |] "fsharpc" with
      | Some(dir,file) -> Some(Path.Combine(dir,file))
      | None ->
      match getShellToolPath [| ""; ".exe"; ".bat" |] "fsc" with
      | Some(dir,file) -> Some(Path.Combine(dir,file))
      | None ->
      match FSharpEnvironment.BinFolderOfDefaultFSharpCompiler None with
      | Some(dir) when FSharpEnvironment.safeExists(Path.Combine(dir, "fsc.exe")) ->
          Some(Path.Combine(dir,"fsc.exe"))
      | _ -> None

  let getDefineSymbols (fileName:string) (project: Project) =
      [if FileSystem.IsAScript fileName
       then yield! ["INTERACTIVE";"EDITING"]
       else yield! ["COMPILED";"EDITING"]

       let configuration =
           match IdeApp.Workspace |> Option.ofNull, project |> Option.ofNull with
           | None, Some proj ->
               //as there is no workspace use the default configuration for the project
               Some (proj.GetConfiguration(proj.DefaultConfiguration.Selector))
           | Some workspace, Some project ->
                 Some (project.GetConfiguration(workspace.ActiveConfiguration))
           | _ -> None

       match configuration with
       | Some config  ->
           match config with
           | :? DotNetProjectConfiguration as config -> yield! config.GetDefineSymbols()
           | _ -> ()
       | None -> () ]

  let getConfig() =
      match MonoDevelop.Ide.IdeApp.Workspace with
            | ws when ws <> null && ws.ActiveConfiguration <> null -> ws.ActiveConfiguration
            | _ -> MonoDevelop.Projects.ConfigurationSelector.Default

  let getArgumentsFromProject (proj:DotNetProject) (config:ConfigurationSelector) (referencedAssemblies) =
        maybe {
            let! projConfig = proj.GetConfiguration(config) |> Option.tryCast<DotNetProjectConfiguration>
            let! fsconfig = projConfig.CompilationParameters |> Option.tryCast<FSharpCompilerParameters>
            return generateProjectOptions (proj, referencedAssemblies, fsconfig, None, getTargetFramework projConfig.TargetFramework.Id, config, false)
        }

  let getReferencesFromProject (proj:DotNetProject, config:ConfigurationSelector, referencedAssemblies) =
        let projConfig = proj.GetConfiguration(config) :?> DotNetProjectConfiguration
        generateReferences(proj, referencedAssemblies, None, getTargetFramework projConfig.TargetFramework.Id, false)