Class: VisualStudio::Product

Inherits:
Object
  • Object
show all
Defined in:
lib/visual_studio/product.rb

Constant Summary collapse

NAMES =
['VC', 'VC#']
PRETTY_NAMES =
['Visual C/C++', 'Visual C#']
NAME_TO_PRETTY_NAME =

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(desc) ⇒ Product

Returns a new instance of Product.



17
18
19
20
21
22
23
24
25
26
27
# File 'lib/visual_studio/product.rb', line 17

def initialize(desc)
  @name      = desc[:name]
  @version   = desc[:version]
  @root      = desc[:root]
  @includes  = desc[:includes]
  @libraries = desc[:libraries]
  @binaries  = desc[:binaries]
  @sdks      = desc[:sdks]
  @supports  = desc[:supports]
  @shared    = desc[:shared]
end

Instance Attribute Details

#binariesObject (readonly)

Returns the value of attribute binaries.



7
8
9
# File 'lib/visual_studio/product.rb', line 7

def binaries
  @binaries
end

#includesObject (readonly)

Returns the value of attribute includes.



7
8
9
# File 'lib/visual_studio/product.rb', line 7

def includes
  @includes
end

#librariesObject (readonly)

Returns the value of attribute libraries.



7
8
9
# File 'lib/visual_studio/product.rb', line 7

def libraries
  @libraries
end

#nameObject (readonly)

Returns the value of attribute name.



7
8
9
# File 'lib/visual_studio/product.rb', line 7

def name
  @name
end

#rootObject (readonly)

Returns the value of attribute root.



7
8
9
# File 'lib/visual_studio/product.rb', line 7

def root
  @root
end

#sdksObject (readonly)

Returns the value of attribute sdks.



7
8
9
# File 'lib/visual_studio/product.rb', line 7

def sdks
  @sdks
end

#sharedObject (readonly)

Returns the value of attribute shared.



7
8
9
# File 'lib/visual_studio/product.rb', line 7

def shared
  @shared
end

#supportsObject (readonly)

Returns the value of attribute supports.



7
8
9
# File 'lib/visual_studio/product.rb', line 7

def supports
  @supports
end

#versionObject (readonly)

Returns the value of attribute version.



7
8
9
# File 'lib/visual_studio/product.rb', line 7

def version
  @version
end

Class Method Details

.find(product, version) ⇒ Object



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
# File 'lib/visual_studio/product.rb', line 99

def self.find(product, version)
  if VisualStudio::Product::NAMES.include?(product)
    name = Helpers::PrettyString.new product,
                                     pretty: VisualStudio::VERSION_TO_PRETTY_NAME[version]

    root = self._find_via_registry(product, version)
    return nil if root.nil?

    # If shared, indicates everything is fucked up and Microsoft can't be

    # trusted. As such, we need to unfuck things downstream.

    shared = root.downcase.include? 'shared'

    includes, libraries, binaries =
      case product
        when 'VC'
          case version.to_f
            when 8.0..11.0
              # TODO(mtwilliams): Check if x86_64 support exists.

              includes  = [File.join(root, 'include')]
              libraries = {:x86    => [File.join(root, 'lib')],
                           :x86_64 => []}
              binaries  = {:x86    => [File.join(root, 'bin')],
                           :x86_64 => []}
              [includes, libraries, binaries]
            when 12.0..14.0
              # TODO(mtwilliams): Select the 64-bit and ARM host variants

              # when applicable, i.e. when running on 64-bit or ARM.

              includes  = [File.join(root, 'include')]
              libraries = {:x86    => [File.join(root, 'lib')],
                           :x86_64 => [File.join(root, 'lib', 'amd64')],
                           :arm    => [File.join(root, 'lib', 'arm')]}
              binaries  = {:x86    => [File.join(root, 'bin')],
                           :x86_64 => [File.join(root, 'bin', 'x86_amd64')],
                           :arm    => [File.join(root, 'bin', 'x86_arm')]}
              [includes, libraries, binaries]
            else
              # TODO(mtwilliams): Raise a proper extension.

              # raise VisualStudio::Unsupported(...)

              raise "Wha-?"
            end
        when 'VC#'
          # TODO(mtwilliams): Determine search paths.

          [[], {}, {}]
        end

    # TODO(mtwilliams): Handle the Xbox One and PS4.

    sdks = (VisualStudio::VERSION_TO_SDKS[version][:windows].map do |version|
              Windows::SoftwareDevelopmentKit.find(version)
            end).compact

    platforms = [:windows]
    architectures = case product
                      when 'VC'
                        case version.to_f
                          when 8.0..11.0
                            # TODO(mtwilliams): Check if x86_64 support exists.

                            [:x86]
                          when 12.0..14.0
                            [:x86, :x86_64, :arm]
                          end
                      when 'VC#'
                        # TODO(mtwilliams): Determine 64-bit support?

                        [:x86, :x86_64, :arm]
                      end

    VisualStudio::Product.new(name: name,
                              version: version,
                              root: root,
                              includes: includes,
                              libraries: libraries,
                              binaries: binaries,
                              sdks: ({windows: sdks}),
                              supports: ({platforms: platforms,
                                          architectures: architectures}),
                              shared: shared)
  else
    # TODO(mtwilliams): Raise an exception.

    # raise VisualStudio::InvalidVersion.new(...)

  end
end

Instance Method Details

#environment(opts = {}) ⇒ Object



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
# File 'lib/visual_studio/product.rb', line 29

def environment(opts={})
  # TODO(mtwilliams): Raise an exception.

  return nil unless @name.to_s == 'VC'

  target = opts[:target] || {platform: :windows,
                             architecture: :x86}

  # TODO(mtwilliams): Handle other platforms.

  # TODO(mtwilliams): Check if the architecture is supported.

  platform = :windows
  arch = {:x86 => 'x86', :x86_64 => 'amd64', :arm => 'arm'}[target[:architecture]]

  # TODO(mtwilliams): Raise an exception.

  return nil unless arch

  if @shared
    # HACK(mtwilliams): Microsoft shipped a broken `vcvarsall.bat`, so

    # we need to build the envrionment ourself.


    root = File.expand_path(File.join(@root, ".."))

    # TODO(mtwilliams): Insert missing variables into environment.

     # WindowsSdkDir

     # WindowsLibPath

     # WindowsSDKVersion

     # UCRTVersion

     # UniversalCRTSdkDir

     # DevEnvDir

     # INCLUDE

     # LIB

     # LIBPATH


    path = []

    # TODO(mtwilliams): Inject latest Windows SDK into PATH.

    case arch
      when 'x86'
        path << File.join(@root, "bin")
      else
        path << File.join(@root, "bin", arch)
      end

    env = {
      "VS140COMNTOOLS" => File.join(root, "Common7", "Tools"),
      "VSINSTALLDIR"   => root,
      "VCINSTALLDIR"   => File.join(root, "VC"),
      "PATH"           => path.join(';')
    }

    env = VisualStudio::Environment.merge(opts[:base] || {}, env)
    env = VisualStudio::Environment.merge(env, opts[:overlay] || {})

    env
  else
    # HACK(mtwilliams): We should reimplement this logic inside this gem.

    require 'open3'
    require 'json'

    cmd   = "call \"#{File.join(@root, 'vcvarsall.bat')}\" #{arch} & " +
            "echo require('json'); print JSON.generate(ENV.to_h); | ruby\n"
    out, _, status = Open3.capture3(ENV.to_h, "cmd.exe /C \"#{cmd}\"")
    return nil unless status == 0

    env = VisualStudio::Environment.merge(opts[:base] || {}, JSON.parse(out))
    env = VisualStudio::Environment.merge(env, opts[:overlay] || {})

    env
  end
end